public Evaluator(EvaluatorItem item) { EvaluatorItem[] items = { item }; ConstructEvaluator(items); }
public Evaluator(EvaluatorItem[] items) { ConstructEvaluator(items); }
private void ConstructEvaluator(EvaluatorItem[] items) { CSharpCodeProvider comp = new CSharpCodeProvider(); 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()); cp.ReferencedAssemblies.Clear(); cp = null; code.Clear(); code = null; 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"); comp = null; cp = null; cr = null; a = null; for (int i = 0; i < items.Length; i++) { items[i] = null; } items = null; }