2016-11-24 54 views
2

我試圖用下一個命令編譯在PowerShell中的C#代碼:Powershell的添加型C#6.0

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

但C#6.0功能無法正常運作,例如:

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

對於代碼:

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

我使用的是最新的PowerShell 5.1.14394.1000

有沒有辦法解決這個問題?

回答

1

當我嘗試霍夫曼先生的做法,我得到以下錯誤:

Add-Type : Could not find a part of the path 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\bin\roslyn\csc.exe'.

user1676558 mentions two solutions

  1. 包含修復此特定問題
  2. 一個snippet of C#一個NuGet package那反映出違規的私人領域並修復它

作爲一個(小聯盟)PowerShell的黑客,我想到了我自己的PowerShellian解決方案的基礎上,source code的檢查:

$dncpTypes = Add-Type -Path C:\<path where I put the dll>\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll -PassThru 
$dncpTypTab = [ordered]@{} 
$dncpTypes | %{$dncpTypTab[$_.Name] = $_} 

$compSetCtor = $dncpTypTab.CompilerSettings.GetConstructor(@([string],[int])) 
$compSettings = $compSetCtor.Invoke(@('C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe', 10)) 
$cscpOtherCtor = $dncpTypTab.CSharpCodeProvider.GetConstructor('NonPublic,Instance', $null, @($dncpTypTab.ICompilerSettings), $null) 
$roslynProvider = $cscpOtherCtor.Invoke($compSettings) 

不用說,有關於這是否是在內胎一些討論一個錯誤。看起來提供者是針對ASP.NET的,並且在那裏做了正確的事情。人們也不同意在哪裏拿起csc.exe。我懷疑這可能會繼續是in flux

[編輯後:在VS2017,CSC似乎住在$ {ENV:PROGRAMFILES(X86)} \微軟的Visual Studio \ 2017年\企業\的MSBuild \ 15.0 \斌\羅斯林\ CSC.EXE]