相关疑难解决方法(0)

PowerShell通用集合

我一直在推动PowerShell中的.NET框架,我遇到了一些我不理解的东西.这很好用:

$foo = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$foo.Add("FOO", "BAR")
$foo

Key                                                         Value
---                                                         -----
FOO                                                         BAR
Run Code Online (Sandbox Code Playgroud)

然而,这不是:

$bar = New-Object "System.Collections.Generic.SortedDictionary``2[System.String,System.String]"
New-Object : Cannot find type [System.Collections.Generic.SortedDictionary`2[System.String,System.String]]: make sure t
he assembly containing this type is loaded.
At line:1 char:18
+ $bar = New-Object <<<< "System.Collections.Generic.SortedDictionary``2[System.String,System.String]"
Run Code Online (Sandbox Code Playgroud)

他们都在同一个集会,所以我错过了什么?

正如答案中指出的那样,这只是PowerShell v1的一个问题.

.net powershell powershell-1.0

36
推荐指数
2
解决办法
4万
查看次数

传入空集会引发错误

HashSet我正在尝试按照答案在我的 powershell 脚本中使用 a 。

Add-Type -AssemblyName System.Core
$set= new-object 'System.Collections.Generic.HashSet[string]'
Run Code Online (Sandbox Code Playgroud)

但是如果我创建一个需要这样一个集合的函数

function foo([Parameter(Mandatory=$true)][Collections.Generic.HashSet[string]]$set){
Run Code Online (Sandbox Code Playgroud)

并传入集合

foo($set)
Run Code Online (Sandbox Code Playgroud)

如果为空,我会收到错误$set

Cannot bind argument to parameter '$set' because it is an empty collection.
    + CategoryInfo          : InvalidData: (:) [script.ps1], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyCollectionNotAllowed,script.ps1
Run Code Online (Sandbox Code Playgroud)

但如果我事先添加一些东西,$set我就不会遇到问题。

为什么参数不能绑定到空集,如何让它绑定到这样的集?

powershell set hashset powershell-4.0

2
推荐指数
1
解决办法
1947
查看次数

标签 统计

powershell ×2

.net ×1

hashset ×1

powershell-1.0 ×1

powershell-4.0 ×1

set ×1