1

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");

Sineng
  • 31
  • 3
  • 1
    post the FULL Exception message. clearly it is trying to find a file that does not exist – Ahmed ilyas Jul 16 '14 at 09:16
  • 1
    Where do you get the exception, and what are the details? (And is there any reason you don't want to use `StringBuilder.AppendLine`?) – Jon Skeet Jul 16 '14 at 09:16
  • i know where the problem is paras.ReferencedAssemblies.Add("System.Data.dll"); it seems i can't add System.Data reference, i don't know why – Sineng Jul 16 '14 at 09:23
  • 1
    You need to fix the bug in your code, checking if the compilation actually succeeded is not optional. Iterate CompilerResults.Errors. Now you *know* why result.CompiledAssembly bombs your program. – Hans Passant Jul 16 '14 at 11:31

0 Answers0