I’m compiling some C# and VB code at run time using the CodeDomProvider, CompilerInfo, and CompilerParameters. It works great, and I really like being able to add scripting support to my application, but it only seems to support .NET 2.0 syntax. For example, the var keyword isn’t supported in C#, and the If(bool, string, string) expression isn’t supported in VB.
How can I tell it to target the 3.5 framework?
OK, I found a big hint here from Anders Norås that there is a constructor for the CSharpCodeProvider constructor that takes some options, including the compiler version. When I checked the MSDN docs, I found that it’s cleaner to specify the compiler options in the App.config file. Here’s an example:
My only disappointment is that now I get two versions of each compiler from CodeDomProvider.GetAllCompilerInfo(). The configuration documentation says that the App.config should override the machine.config settings, but I get both. So far, the App.config is always after the machine.config, so I just let the later ones override the earlier ones. Anybody know how to avoid the duplication?