在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
public class Expression { object instance; MethodInfo method; /// <summary> /// 表达试运算 /// </summary> /// <param name="expression">表达试</param> public Expression(string expression) { if (expression.IndexOf("return") < 0) expression = "return " + expression + ";"; string className = "Expression"; string methodName = "Compute"; CompilerParameters p = new CompilerParameters(); p.GenerateInMemory = true; CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string. Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}", className, methodName, expression)); if (cr.Errors.Count > 0) { string msg = "Expression(\"" + expression + "\"): \n"; foreach (CompilerError err in cr.Errors) { msg += err.ToString() + "\n"; } throw new Exception(msg); } instance = cr.CompiledAssembly.CreateInstance(className); method = instance.GetType().GetMethod(methodName); } /// <summary> /// 处理数据 /// </summary> /// <param name="x"></param> /// <returns>返回计算值</returns> public double Compute(double x) { return (double)method.Invoke(instance, new object[] { x }); } } class Program { static void Main(string[] args) { Expression e = new Expression("x+5*9/3.2+6"); Console.WriteLine(e.Compute(10)); Console.Read(); } }
|
请发表评论