.net 我可以在powershell中使用System.Core.dll/System.Collections.Generic.HashSet吗?
我想在powershell脚本中使用哈希集。我想我已经了解了如何通过以下操作实例化通用集合对象:.net 我可以在powershell中使用System.Core.dll/System.Collections.Generic.HashSet吗?,.net,.net-3.5,powershell,.net,.net 3.5,Powershell,我想在powershell脚本中使用哈希集。我想我已经了解了如何通过以下操作实例化通用集合对象: [type] $strType = "string" $listClass = [System.Collections.Generic.List``1] $listObject = $base.MakeGenericType(@($t)) $myList = New-Object $setObject 这对于列表和字典很好,但是当我尝试创建哈希集时,我得到: Unable to find type
[type] $strType = "string"
$listClass = [System.Collections.Generic.List``1]
$listObject = $base.MakeGenericType(@($t))
$myList = New-Object $setObject
这对于列表和字典很好,但是当我尝试创建哈希集时,我得到:
Unable to find type [System.Collections.Generic.HashSet`1]: make sure that the assembly containing this type is loaded.
所以现在看起来我需要加载System.Core.dll,但我似乎无法让powershell加载该程序集。例如,调用[System.Reflection.Assembly]::LoadWithPartialName(“System.Core”)会导致此异常:
"LoadWithPartialName" with "1" argument(s): "Could not load file or assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified."
有任何指针吗?PowerShell 2.0通过1)添加用于加载程序集的Add-Type cmdlet和2)更新语法使指定封闭的泛型类型名称变得更简单,从而简化了此过程,例如:
PS> Add-Type -AssemblyName System.Core
PS> $h = new-object 'System.Collections.Generic.HashSet[string]'
PS> $h.Add('f')
True
您是否在powershell v1或v2中?我在Win2k8 R2上,主机显示版本2.0可能重复:请参阅答案,然后我可以通过执行
[参数(强制=$true)][hashset]
指定函数中的参数类型吗?是,但您需要将参数类型指定为[Collections.Generic.hashset[string]]
替换需要存储在哈希集中的字符串类型。