我正在使用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");第二届前
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
发布于 2015-05-05 13:41:43
如果我们正在编译的源代码有任何错误,那么CodeDOM在编译后删除dll,或者它永远不会创建它。在每种情况下,CompiledAssembly的值都是空的,所以请确保正在编译的代码。在进行编译后的程序集检查错误之前,如果是任何错误,请在获取所创建的dll之前中断。
https://stackoverflow.com/questions/30053490
复制相似问题