3

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> { { "CompilerVersion", "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?

2
  • Mike what are args for? You never use them. Commented Jan 22, 2013 at 15:55
  • I have no idea, it has been a long time. I probably made an error when anonymizing the code. Commented Jan 25, 2013 at 16:02

2 Answers 2

3

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());
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.