问题描述
我正在尝试编译 c#powershell 中的源代码,使用以下命令:>
I am trying to compile c# source code in powershell by using this command:
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source
但是 c#-6.0 功能不起作用,例如:
But c#-6.0 features are not working, for example:
Add-Type : c:\Users\...\AppData\Local\Temp\2\d2q5hn5b.0.cs(101) : Unexpected character '$'
对于代码:
new Problem($"... ({identifier})", node)
我正在使用 powershell-v5.1
有没有办法解决这个问题?
Is there a way to fix this?
推荐答案
Powershell 使用 CodeDomProvider
来编译它们的程序集. 框架提供的版本只支持 C# 5,因此默认情况下没有新功能可用.
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.
但是,如果您提供另一个 CodeDomProvider
,您可以编译任何语言,包括 C#6.有一个 CodeDomProvider
可用于 Roslyn(新的 .NET 编译器).您可以从 NuGet 下载 并使用 包含程序集添加类型
.然后创建一个编译器实例并将其传递到 -CodeDomProvider
属性中.
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.
这篇关于`Add-Type` C# 6+ 特性抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!