首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeDOM -编译失败

CodeDOM -编译失败
EN

Stack Overflow用户
提问于 2015-05-05 12:50:20
回答 1查看 743关注 0票数 0

我正在使用CodeDOM编译代码。首先,它非常完美地编译,但现在我在代码行16上遇到了一个问题:

代码语言:javascript
复制
 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       }

我得到的错误是:

代码语言:javascript
复制
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源代码是什么?

对不起,我忘了附上所有的课程,在这里你可以完全看到:

代码语言:javascript
复制
 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例

代码语言:javascript
复制
   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");

第二届前

代码语言:javascript
复制
   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

EN

回答 1

Stack Overflow用户

发布于 2015-05-05 13:41:43

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30053490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档