Paul Alexander
Paul Alexander

Reputation: 32367

CSharpCodeProvider output .NET 2.0 assembly from .NET 4.0 application

I'm using the CSharpCodeProvider to compile a CodeDom object into an assembly. The application itself is running under .NET 4.0. However I need the output from CompileAssemblyFromDom to build against .NET 2.0 for compatibility with some external resources. How can I tell the CSharpCodeProvider to build against .NET 2.0?

Upvotes: 3

Views: 639

Answers (1)

Nicole Calinoiu
Nicole Calinoiu

Reputation: 20992

You can provider the compiler version as an option via the CSharpCodeProvider constructor that takes a providerOptions (IDictionary) argument. If you're using CodeDomProvider.CreateProvider, you can use its similar overload. e.g.:

using (CodeDomProvider provider = CodeDomProvider.CreateProvider(
    "CSharp",
    new Dictionary<string, string>() { { "CompilerVersion", "v2.0" } }))
{
    //...
}

The compiler version can also be specified via a configuration file. See http://msdn.microsoft.com/en-us/library/bb537926.aspx for details and examples.

Upvotes: 5

Related Questions