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.
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.
<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.
compiler language="c#;cs;csharp"
? – Buckingham