I'm using VS2010, as soon as i add that line " DataTable table= null;",an error occured here is the code:
public static object GetLambdaResult(string expression)
{
string className = "ExecuteLambda";
string methodName = "Run";
var providerDic = new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } };
ICodeCompiler complier = (new CSharpCodeProvider(providerDic).CreateCompiler());
CompilerParameters paras = new CompilerParameters();
paras.GenerateExecutable = false;
paras.GenerateInMemory = true;
paras.ReferencedAssemblies.Add("System.dll");
paras.ReferencedAssemblies.Add("System.Data.dll");
StringBuilder classSource = new StringBuilder();
classSource.Append("using System; \n");
classSource.Append("using System.Data; \n");
classSource.Append("namespace AutoRun \n");
classSource.Append("{\n");
classSource.AppendFormat(" public class {0} \n", className);
classSource.Append(" {\n");
classSource.AppendFormat(" public static object {0}()\n", methodName);
classSource.Append(" {\n");
classSource.Append(" DataTable table= null;\n");
classSource.AppendFormat(" return {0};\n", expression);
classSource.Append(" }\n");
classSource.Append(" }\n");
classSource.Append("}");
CompilerResults result = complier.CompileAssemblyFromSource(paras, classSource.ToString());
Assembly assembly = result.CompiledAssembly;
object eval = assembly.CreateInstance("AutoRun." + className);
MethodInfo method = eval.GetType().GetMethod(methodName);
object reobj = method.Invoke(eval, null);
GC.Collect();
return reobj;
}
and i get a FileNotFoundException, {"Could not load file or assembly" file :/ / / C: \ \ Users \ \ Administrator \ \ AppData \ \ Local \ \ Temp \ \ 12azlzyr.dll "or one of its dependencies
. System can not find the file specified. ":" file :/ / / C: \ \ Users \ \ Administrator \ \ AppData \ \ Local \ \ Temp \ \ 12azlzyr.dll "
the problem is
classSource.Append(" DataTable table= null;\n");
and
paras.ReferencedAssemblies.Add("System.Data.dll");
i don't know how to solve yet
After a long time search, i find the solution, need to add xml reference paras.ReferencedAssemblies.Add("System.XML.dll");