创建一个PowerShell Collections.Generic.IEnumerable[string]错误。

问题描述 投票:0回答:1

我无法在PowerShell上创建一个IEnumerable,我的目标是另一个issueetype,但帮我解决这个 "简单 "的问题。

我的目标是另一个issueetype,但帮我解决这个 "简单 "的问题。

C:\Users\Me> $var = New-Object Collections.Generic.IEnumerable[string]
New-Object : A constructor was not found. Cannot find an appropriate constructor for type Collections.Generic.IEnumerable[string].
At line:1 char:8
+ $var = New-Object Collections.Generic.IEnumerable[string]
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

有谁能说明我哪里做错了?

最后的对象有一个属性是 Collections.Generic.IEnumerable[...]。 所以这就是为什么使用这种类型。

powershell ienumerable
1个回答
0
投票

完整的类型名称是 System.Collections.Generic.IEnumerable[string]:

$var = New-Object System.Collections.Generic.IEnumerable[string]

如果你使用PowerShell 5.0及更新版本的软件 ::new() 方法,你可以跳过 System 命名空间前缀。

$var = [Collections.Generic.IEnumerable[string]]::new()
# these will resolve to the same type
$var = [System.Collections.Generic.IEnumerable[string]]::new()
© www.soinside.com 2019 - 2024. All rights reserved.