Dot-Net

PowerShell 中的 IEnumerable<CustomType>

  • April 28, 2013

我正在嘗試Enumerable.ToList()在 PowerShell 中使用。顯然,要做到這一點,我必須明確地將對象轉換為IEnumerable&lt;CustomType&gt;,但我無法做到這一點。看來我無法IEnumerable&lt;CustomType&gt;在 PowerShell 中正確編寫。兩者都IEnumerable&lt;string&gt;可以CustomType正常工作(我嘗試使用的自定義類型稱為WpApiLib.Page),所以我不知道我做錯了什麼。

PS C:\Users\Svick&gt; [Collections.Generic.IEnumerable``1[System.String]]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    IEnumerable`1


PS C:\Users\Svick&gt; [WpApiLib.Page]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Page                                     System.Object


PS C:\Users\Svick&gt; [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]] &lt;&lt;&lt;&lt;

在此處查看答案:Powershell Generic Collections。看起來這是 PowerShell 如何解釋泛型的不幸結果,您需要完全限定WpApiLib.Page類型。

在 PowerShell v2 中,您可以使用

Collections.Generic.IEnumerable[string]

參考IEnumberable&lt;string&gt;。該語法適用於New-Object(儘管不適用於介面),並且似乎也適用於強制轉換。

引用自:https://stackoverflow.com/questions/2495156