Zergatul Zergatul - 7 months ago 85
C# Question

Powershell Add-Type C# 6.0

I am trying to compile C# source code in powershell by using next command:

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -Language CSharp

But C# 6.0 features are not working, for example:

Add-Type : c:\Users\...\AppData\Local\Temp\2\d2q5hn5b.0.cs(101) : Unexpected character '$'

For code:

new Problem($"... ({identifier})", node)

I am using the latest powershell

Is there a way to fix this?


Powershell uses CodeDomProvider to compile their assemblies. The version provided with the framework just supports C# 5, so no new features are available by default.

However, if you provide another CodeDomProvider, you can compile any language, also C#6. There is a CodeDomProvider available for Roslyn (the new .NET compiler). You can download it from NuGet and include the assembly using Add-Type. Then create an instance of the compiler and pass that in the -CodeDomProvider attribute.