打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Runtime C# Expression Evaluator
           <!-- Download Links --><!-- Add the rest of your HTML here -->

Introduction

This is a simple class library (or just .cs file if you wish) to allow for runtime compilation and evaluation of C# code blocks. There are both static methods of the Evaluator class that allow for simple use (but at a performance penalty) or you can use the object directly to create multiple evaluations:

Hide   Copy Code
Console.WriteLine("Test0: {0}", Evaluator.EvaluateToInteger("(30 + 4) * 2"));Console.WriteLine("Test1: {0}", Evaluator.EvaluateToString("\"Hello \" + \"There\""));Console.WriteLine("Test2: {0}", Evaluator.EvaluateToBool("30 == 40"));Console.WriteLine("Test3: {0}", Evaluator.EvaluateToObject("new DataSet()"));EvaluatorItem[] items = {                          new EvaluatorItem(typeof(int), "(30 + 4) * 2", "GetNumber"),                          new EvaluatorItem(typeof(string), "\"Hello \" + \"There\"",                                                             "GetString"),                          new EvaluatorItem(typeof(bool), "30 == 40", "GetBool"),                          new EvaluatorItem(typeof(object), "new DataSet()", "GetDataSet")                        };Evaluator eval = new Evaluator(items);Console.WriteLine("TestStatic0: {0}", eval.EvaluateInt("GetNumber"));Console.WriteLine("TestStatic1: {0}", eval.EvaluateString("GetString"));Console.WriteLine("TestStatic2: {0}", eval.EvaluateBool("GetBool"));Console.WriteLine("TestStatic3: {0}", eval.Evaluate("GetDataSet"));

How does it work? I am using the CodeDOM to create a simple assembly with a single class in it. I simply transform each of the EvaluatorItems into a method of the class. When you call EvaluateInt() or Evaluate(), I use reflection to get a MethodInfo object and call the method.

The source code comes packaged in a Class Library with a Test program that you can use to get examples of use:

To compile the expressions, I am creating a new CSharpCodeProvider and setting Compiler attributes (like adding references, telling it to generate it in memory, etc.). Then I am building a dummy class that I can append my methods on. Lastly I compile it (check for errors) and save the object to use to call with the MethodInfo structure:

Hide   Shrink
  Copy Code
ICodeCompiler comp = (new CSharpCodeProvider().CreateCompiler());CompilerParameters cp = new CompilerParameters();cp.ReferencedAssemblies.Add("system.dll");cp.ReferencedAssemblies.Add("system.data.dll");cp.ReferencedAssemblies.Add("system.xml.dll");cp.GenerateExecutable = false;cp.GenerateInMemory = true;StringBuilder code = new StringBuilder();code.Append("using System; \n");code.Append("using System.Data; \n");code.Append("using System.Data.SqlClient; \n");code.Append("using System.Data.OleDb; \n");code.Append("using System.Xml; \n");code.Append("namespace ADOGuy { \n");code.Append("  public class _Evaluator { \n");foreach(EvaluatorItem item in items){  code.AppendFormat("    public {0} {1}() ",                     item.ReturnType.Name,                     item.Name);  code.Append("{ ");  code.AppendFormat("      return ({0}); ", item.Expression);  code.Append("}\n");}code.Append("} }");CompilerResults cr = comp.CompileAssemblyFromSource(cp, code.ToString());if (cr.Errors.HasErrors){  StringBuilder error = new StringBuilder();  error.Append("Error Compiling Expression: ");  foreach (CompilerError err in cr.Errors)  {    error.AppendFormat("{0}\n", err.ErrorText);  }  throw new Exception("Error Compiling Expression: " + error.ToString());}Assembly a = cr.CompiledAssembly;_Compiled = a.CreateInstance("ADOGuy._Evaluator");

When I call the _Compiled object, I use a MethodInfo object to Invoke the call and return the result to the user:

Hide   Copy Code
public object Evaluate(string name){  MethodInfo mi = _Compiled.GetType().GetMethod(name);  return mi.Invoke(_Compiled, null); 
}请选中你要保存的内容,粘贴到此文本框
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 将字符串解析运算并返回结果
NET Foundations - .NET execution model - VusCode - Coding dreams since 1998!
Unsafe Code Tutorial (C#)
分解质因数
检索出 IList<T> 或 List<T>中的不重复数据 Distinct()
compiler construction
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服