Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<CustomType> in PowerShell

I'm trying to use Enumerable.ToList() in PowerShell. Apparently, to do that, I have to explicitly convert the object to IEnumerable<CustomType>, but I am unable to do that. It seems I can't correctly write IEnumerable<CustomType> in PowerShell. Both IEnumerable<string> and CustomType itself work correctly (the custom type I'm trying to use is called WpApiLib.Page), so I don't know what can I be doing wrong.

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]] <<<<
like image 388
svick Avatar asked Mar 22 '10 19:03

svick


2 Answers

In PowerShell v2 you can use

Collections.Generic.IEnumerable[string]

to refer to IEnumberable<string>. The syntax works for New-Object (though not with an interface) and it seems to be valid for casts, too.

like image 166
Joey Avatar answered Sep 21 '22 18:09

Joey


See the answer here: Powershell Generic Collections. It looks like this is an unfortunate consequence of how PowerShell interprets generics, and you'll need to fully qualify the WpApiLib.Page type.

like image 25
kvb Avatar answered Sep 22 '22 18:09

kvb