简体   繁体   中英

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?

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. eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM