6

I would like to use the Microsoft.CSharp.CSharpCodeProvider class to compile C# 7.3 code. The compiler version is specified in an IDictionary that is taken as input when a new CSharpCodeProvider is created; for example, { "CompilerVersion", "v4.0" }. "v4.0" is not sufficient, as it does not recognize v7.3 as a compiler option.

Narf the Mouse
  • 1,541
  • 5
  • 18
  • 30

1 Answers1

10

The newer compiler versions are no longer shipped as part of the .NET Framework proper, and therefore cannot be by default accessed via the legacy CodeDOM API (which includes Microsoft.CSharp.CSharpCodeProvider).

Instead if you wish to use the CodeDOM API with the newer compilers, you want to use Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider which is a subclass of Microsoft.CSharp.CSharpCodeProvider. This class is available in the Microsoft.CodeDom.Providers.DotNetCompilerPlatform nuget package.

For non web applciations you also need to provide a configuration or environment variable that provides a path to C# compiler you want to use (A copy ships in the nuget package, so you can use that). See https://github.com/aspnet/RoslynCodeDomProvider for details.

Kevin Cathcart
  • 9,838
  • 2
  • 36
  • 32
  • For web.config ? `compiler language="c#;cs;csharp"` ? – Kiquenet Jul 19 '21 at 13:45
  • 1
    @Kiquenet When you add the nuget package, it would normally run powershell script to update web.config. If not, what you want is: ``. Of course, there are things you can adjust in that if needed. – Kevin Cathcart Jul 20 '21 at 14:35