Dot-Net
PowerShell 中的 IEnumerable<CustomType>
我正在嘗試
Enumerable.ToList()在 PowerShell 中使用。顯然,要做到這一點,我必須明確地將對象轉換為IEnumerable<CustomType>,但我無法做到這一點。看來我無法IEnumerable<CustomType>在 PowerShell 中正確編寫。兩者都IEnumerable<string>可以CustomType正常工作(我嘗試使用的自定義類型稱為WpApiLib.Page),所以我不知道我做錯了什麼。PS C:\Users\Svick> [Collections.Generic.IEnumerable``1[System.String]] IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False IEnumerable`1 PS C:\Users\Svick> [WpApiLib.Page] IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Page System.Object PS C:\Users\Svick> [Collections.Generic.IEnumerable``1[WpApiLib.Page]] Unable to find type [Collections.Generic.IEnumerable`1[WpApiLib.Page]]: make su re that the assembly containing this type is loaded. At line:1 char:51 + [Collections.Generic.IEnumerable``1[WpApiLib.Page]] <<<<
在此處查看答案:Powershell Generic Collections。看起來這是 PowerShell 如何解釋泛型的不幸結果,您需要完全限定
WpApiLib.Page類型。
在 PowerShell v2 中,您可以使用
Collections.Generic.IEnumerable[string]參考
IEnumberable<string>。該語法適用於New-Object(儘管不適用於介面),並且似乎也適用於強制轉換。