简体   繁体   中英

How do I run C# 4.0 compiler with CSharpCodeProvider class?

I'm using this snippet

CSharpCodeProvider codeProvider =
   new  CSharpCodeProvider(
      new Dictionary<String, String> { { "CompilerVersion", "v3.5" } });

for the 3.5 compiler for dynamic code compilation.

I'm assuming I should use "v4.0" to switch to the 4.0 compiler?

Besides specifying "v4.0", you will need to add the reference "Microsoft.CSharp.dll" in order to use the "dynamic" keyword.

var codeProvider = new Microsoft.CSharp.CSharpCodeProvider(
new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
var parameters = new System.CodeDom.Compiler.CompilerParameters 
{
    GenerateInMemory = true,
    GenerateExecutable = false,
    IncludeDebugInformation = true,
    TreatWarningsAsErrors = false
};
// Here add more referenced assemblies
parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");

Don't forget to compile the solution using the Framework 4.0+.

I hope this helps.

确实是v4.0

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