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

1

7

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.

Cutin answered 8/5, 2018 at 19:28 Comment(0)
A
12

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.

Airel answered 8/5, 2018 at 20:6 Comment(2)
For web.config ? compiler language="c#;cs;csharp" ?Buckingham
@Buckingham When you add the nuget package, it would normally run powershell script to update web.config. If not, what you want is: <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:7.0 /nowarn:1659;1699;1701;612;618" />. Of course, there are things you can adjust in that if needed.Airel

© 2022 - 2024 — McMap. All rights reserved.