Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C# compiler version to compile C# 7.3 with the CSharpCodeProvider class?

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.

like image 276
Narf the Mouse Avatar asked Jan 28 '23 03:01

Narf the Mouse


1 Answers

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.

like image 152
Kevin Cathcart Avatar answered Feb 06 '23 11:02

Kevin Cathcart