开发者

CodeDom compiler: Type or namespace 'Foo' does not exist in the namespace 'System'

开发者 https://www.devze.com 2023-03-15 04:50 出处:网络
I\'m writing a tool that generates C# code, and am writing unit tests to go with it. One of the tests I want to do is to run the tool and compile the results, so I can examine the assembly with refle

I'm writing a tool that generates C# code, and am writing unit tests to go with it.

One of the tests I want to do is to run the tool and compile the results, so I can examine the assembly with reflection. However, I am having trouble doing this. My current code looks like this:

var args = new Dictionary<string, string> { { "C开发者_如何学编程ompilerVersion", "v4.0" } };
var compiler = new CSharpCodeProvider();

var parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
parameters.ReferencedAssemblies.Add("System.Data.dll");
parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
parameters.ReferencedAssemblies.Add("System.Xml.dll");
parameters.ReferencedAssemblies.Add("System.Xml.Linq.dll");

var results = compiler.CompileAssemblyFromFile(new CompilerParameters(), Directory.GetFiles("Model\\Editable").Concat(Directory.GetFiles("Model\\NotEditable")).ToArray());

if (results.Errors.Count > 0) {
    StringBuilder sb = new StringBuilder();

    foreach (CompilerError err in results.Errors) {
        sb.AppendLine(err.FileName + "(" + err.Line + ":" + err.Column + "): " + err.ErrorText);
    }

    throw new Exception(sb.ToString());
}

The exception is not normally part of the test, but just allows me to view what errors are spit out. Sadly, the errors I'm hitting are along the lines of:

c:\<redacted>\bin\Debug\Model\NotEditable\Computer.cs(5:14): The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\<redacted>\bin\Debug\Model\NotEditable\Computer.cs(6:14): The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\<redacted>\bin\Debug\Model\NotEditable\Computer.cs(7:14): The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\<redacted>\bin\Debug\Model\NotEditable\Computer.cs(8:14): The type or namespace name 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?)

Am I adding these references correctly, or am I doing something wrong?


Looks like you should pass parameters to CompileAssemblyFromFile, not a new instance of CompilerParameters.

var results = compiler.CompileAssemblyFromFile(parameters, Directory.GetFiles("Model\\Editable").Concat(Directory.GetFiles("Model\\NotEditable")).ToArray());


In your sample code, you are not using the CompilerParameters you initialized.

var results = compiler.CompileAssemblyFromFile(parameters, Directory.GetFiles("Model\\Editable").Concat(Directory.GetFiles("Model\\NotEditable")).ToArray());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号