gpt4 book ai didi

powershell - powershell:创建具有语法错误的通用哈希集?

转载 作者:行者123 更新时间:2023-12-03 01:15:38 24 4
gpt4 key购买 nike

我在Powershell 4.0中尝试过此操作:

[System.Assembly]::LoadWithPartialName("System.Collections.Generic")
$m = New-Object '[System.Collections.Generic::HashSet](String)'
$m.GetType()

但是运行会出错。我的用法不正确吗?我只有两行代码!
Unable to find type [System.Assembly]. Make sure that the assembly that contains this type is loaded.
At D:\Untitled1.ps1:1 char:1
+ [System.Assembly]::LoadWithPartialName("System.Collections.Generic")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Assembly:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

New-Object : Cannot find type [[System.Collections.Generic::HashSet] (String)]: verify that the assembly containing this type is loaded.
At D:\Untitled1.ps1:2 char:6
+ $m = New-Object '[System.Collections.Generic::HashSet](String)'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At D:\Untitled1.ps1:3 char:1
+ $m.GetType()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

最佳答案

LoadWithPartialName()在[System.Reflection.Assembly]中,而不在[System.Assembly]中。另外,您的New-Object语法略有不同。

[System.Reflection.Assembly]::LoadWithPartialName("System.Collections.Generic")
$m = New-Object 'System.Collections.Generic.HashSet[String]'
$m.GetType()

另请参阅: http://www.leeholmes.com/blog/2006/08/18/creating-generic-types-in-powershell/

关于powershell - powershell:创建具有语法错误的通用哈希集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30553578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com