[英]How to compile C# / VB code with string interpolation using PowerShell?
我在Windows 10.0.18363.959 和PowerShell 5.1.18362.752
嘗試在PowerShell中編譯C#或VB.NET代碼時,如下所示:
$Source = @'
Imports Microsoft.VisualBasic
Imports System
Public NotInheritable Class MainClass
Public Shared Sub Main()
Console.WriteLine($"{True}")
End Sub
End Class
'@
$vbType = Add-Type -TypeDefinition $Source `
-CodeDomProvider (New-Object Microsoft.VisualBasic.VBCodeProvider) `
-PassThru `
-ReferencedAssemblies "Microsoft.VisualBasic.dll", `
"System.dll" `
| where { $_.IsPublic }
[MainClass]::Main()
$Console = [System.Console]
$Console::WriteLine("Press any key to exit...")
$Console::ReadKey($true)
Exit(0)
我收到編譯器錯誤,因為“ $ ”字符用於字符串插值:
C:\Users\Administrador\AppData\Local\Temp\oshdpbp1\oshdpbp1.0.vb(12): >>> 控制台.WriteLine($"{1}")
我知道我可以改用String.Format() function,但我想知道是否可以在不修改原始 VB.NET 代碼(當然它在Visual Studio上編譯正確)的情況下解決這個問題。
請注意,在VB14中添加了字符串插值。 也許我不知道如何指定正確的 VB 編譯器版本,我不知道如何使用 PowerShell 來指定。
VBCodeProvider
有一個帶有IDictionary
參數的構造函數,允許您指定編譯器版本。
這可能有效,但我現在無法測試:
$provOptions = [System.Collections.Generic.Dictionary[string,string]]::new()
$provOptions['CompilerVersion'] = 'v14.0'
$vbType = Add-Type -TypeDefinition $Source `
-CodeDomProvider (New-Object Microsoft.VisualBasic.VBCodeProvider $provOptions) `
-PassThru `
-ReferencedAssemblies "Microsoft.VisualBasic.dll", `
"System.dll" `
| where { $_.IsPublic }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.