Search code examples
c#powershellroslyn

`Add-Type` C# 6+ features throwing errors


I am trying to compile source code in by using this command:

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source

But 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

Is there a way to fix this?


Solution

  • 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.