powershell - 在Powershell中创建通用列表时出现“Could not construct Closed Type using the provided arguments”错误

标签 powershell

我有一个Powershell脚本,我想在其中创建在自定义(C++ / CLI)程序集中定义的类型的通用列表。

如果运行new-object My.Namespace.MyClass,则可以整天创建对象。但是,如果运行new-genericobject System.Collections.Generic.List -Of My.Namespace.MyClass,则会出现以下错误:

New-GenericObject : Could not construct Closed Type using the provided arguments.
At line:1 char:18
+ new-genericobject <<<<  System.Collections.Generic.List -Of My.Namespace.MyClass
    + CategoryInfo          : NotSpecified: (:) [New-GenericObject], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Pscx.Commands.NewGenericObjectCommand

我可以创建字符串列表,因此new-genericobject的基本功能很好。有没有人知道此错误的含义以及如何解决?

最佳答案

您可以尝试以下方法吗:

$list = new-object 'collections.generic.list[my.namespace.myclass]'

顺便说一句,您正在使用哪个版本的Powershell?什么操作系统?

更新

这可能是一个长期运行的错误,其中,如果元素类型在第三方程序集中,而集合不在同一程序集中,则必须完全限定该元素类型。试试这个:
$list = new-object ("collections.generic.list[[{0}]]" `
    -f [MyClass].AssemblyQualifiedName)

关于powershell - 在Powershell中创建通用列表时出现“Could not construct Closed Type using the provided arguments”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8004438/

相关文章:

powershell - 通过 powershell 挂载 VHD 引发 ResourceBusy 错误

.net - 在 Powershell (V5) 类中使用 .NET 对象

PowerShell 作业与启动进程

json - Invoke-RestMethod Powershell V3内容类型

Powershell接收作业: Quota violation when invoking command as a job

c# - 从 C# 以 32 位或 64 位运行 PowerShell

sql - Azure SQL DB : create users with PowerShell, 参数化 SQL 脚本和 Azure Key Vault

wpf - Powershell 捕获不同变量中的输出和详细信息

powershell - 关于迭代的哈希表与自定义对象数组

powershell - 运行 Get-ADUser 数组的速度非常慢 - 我可以加快速度吗?