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?
+3
A:
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.
Nicole Calinoiu
2010-09-20 13:47:10