CodeDOM - 编译失败

时间:2015-05-05 12:50:21

标签: c# compilation codedom

我正在使用CodeDOM编译代码。首先它完全编译,但现在我在代码行16遇到问题:

 1        public void GenerateAssembly (bool inMemory, string assemblyName) {
 2               CompilerParameters cp = new CompilerParameters();
 3               cp.GenerateExecutable = false;
 4               cp.GenerateInMemory = inMemory;
 5               cp.OutputAssembly = assemblyName;
 6               cp.ReferencedAssemblies.Add("mscorlib.dll");
 7               cp.ReferencedAssemblies.Add("System.dll");
 8               cp.ReferencedAssemblies.Add("System.Core.dll");
 9               cp.ReferencedAssemblies.Add("System.Data.Linq.dll");
10               cp.ReferencedAssemblies.Add("System.Data.Entity.dll");
11               var providerOptions = new Dictionary<string, string>();
12               providerOptions.Add("CompilerVersion", "v4.0");
13               CodeDomProvider compiler = CodeDomProvider.CreateProvider("C#", providerOptions);
14               compiler.CompileAssemblyFromSource(compilationParameters, sourceCode)
15               buildErrors = compilationResult.Errors;
16               lastBuild = compilationResult.CompiledAssembly;
17       }

我得到的错误是:

FileNotFoundException
Could not load file or assembly 'file:///[project_location]\bin\Debug\Dynamic.dll'
or one of its dependencies. The system cannot find the file specified.

编辑1 什么是源代码?

抱歉,我忘了附上所有课程,在这里你可以完全看到它:

 1 public class CustomCompile {
 2        private string[] sourceCode;
 3        public string[] SourceCode { get { return sourceCode; } }
 4        public CompilerErrorCompilation buildErrors;
 5        public Assembly lastBuild;
 6        public CustomCompile (string[] sourceCode) {
 7               this.sourceCode = sourceCode;
 8        }
 9        public void GenerateAssembly (bool inMemory, string assemblyName) {
10               CompilerParameters cp = new CompilerParameters();
11               cp.GenerateExecutable = false;
12               cp.GenerateInMemory = inMemory;
13               cp.OutputAssembly = assemblyName;
14               cp.ReferencedAssemblies.Add("mscorlib.dll");
15               cp.ReferencedAssemblies.Add("System.dll");
16               cp.ReferencedAssemblies.Add("System.Core.dll");
17               cp.ReferencedAssemblies.Add("System.Data.Linq.dll");
18               cp.ReferencedAssemblies.Add("System.Data.Entity.dll");
19               var providerOptions = new Dictionary<string, string>();
20               providerOptions.Add("CompilerVersion", "v4.0");
21               CodeDomProvider compiler = CodeDomProvider.CreateProvider("C#", providerOptions);
22               compiler.CompileAssemblyFromSource(compilationParameters, sourceCode)
23               buildErrors = compilationResult.Errors;
24               lastBuild = compilationResult.CompiledAssembly;
25       }
26 }

编辑2 实施例

   var scriptA = "using System;\nusing System.Collection;\nclass Test {\n\tvoid Init () { Console.WriteLine (\"Void Init got called\"); }\n}";
   var compile = new CustomCompile(new string[] {});
   compile.GenerateAssembly (false, "Dynamic.dll");
   // -- or --
   compile.GenerateAssembly (true, "Dynamic.dll");

第2前

   var compile = new CustomCompile(new string[] {System.IO.File.ReadAllText ("C:\\Users\\[Username]\\Desktop\\CommandExecuter - Copy.cs")});
   compile.GenerateAssembly (false, "Dynamic.dll");
   // -- or --
   compile.GenerateAssembly (true, "Dynamic.dll");

http://pastebin.com/PGWbKFdD

1 个答案:

答案 0 :(得分:0)

如果我们编译的源代码有任何错误,那么CodeDOM会在编译后删除dll,或者它可能永远不会创建它。在每种情况下,CompiledAssembly的值都为null,因此请确保要编译的代码。在进行编译程序集检查错误之前,在我们任何一个的情况下,在获取创建的dll之前中断。