代码之家  ›  专栏  ›  技术社区  ›  svick Raja Nadar

PowerShell中的IEnumerable<CustomType>

  •  7
  • svick Raja Nadar  · 技术社区  · 15 年前

    我想用 Enumerable.ToList() 在PowerShell中。显然,要做到这一点,我必须将对象显式转换为 IEnumerable<CustomType> 但是我做不到。我好像写不好 IEnumerable<自定义类型> 在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]] <<<<
    
    2 回复  |  直到 12 年前
        1
  •  3
  •   Community CDub    8 年前

    请参阅以下答案: Powershell Generic Collections . 看起来这是PowerShell如何解释泛型的一个不幸结果,您需要完全限定 WpApiLib.Page 类型。

        2
  •  6
  •   Joey Gumbo    15 年前

    在PowerShell v2中,可以使用

    Collections.Generic.IEnumerable[string]
    

    参考 IEnumberable<string> . 语法适用于 New-Object (虽然没有接口)而且它似乎对强制转换也是有效的。

    推荐文章