.net 复杂字典的foreach循环不正确

.net 复杂字典的foreach循环不正确,.net,loops,powershell,dictionary,.net,Loops,Powershell,Dictionary,我正在使用Windows7和PowerGUI脚本编辑器来编写ps1。 以下是我的部分代码: #In global scope $Type_Trans = "System.Collections.Generic.Dictionary[System.String,PSObject]" $Type_Farms = "System.Collections.Generic.Dictionary[System.Int32,$Type_Trans]" $Dic_Counts = New-Object $Ty

我正在使用Windows7和PowerGUI脚本编辑器来编写ps1。 以下是我的部分代码:

#In global scope
$Type_Trans = "System.Collections.Generic.Dictionary[System.String,PSObject]"
$Type_Farms = "System.Collections.Generic.Dictionary[System.Int32,$Type_Trans]"

$Dic_Counts = New-Object $Type_Farms

#...puts some data in $Dic_Counts here...
#It is no problem with printing out it in console

#Now call the function below
Write-Data $Dic_Counts

Function Write-Data
{
    param(
        $Dic_Counts
    )

    Foreach($Dic_CountsSingle in $Dic_Counts)
    {
        Write-DataSingle $Dic_CountsSingle  #THIS LINE!
    }
}
这里很奇怪:
Dic_Counts single
为什么不是
KeyValuePair
,而是与
Dic_Counts
一样

多谢各位

使用

foreach ($Dic_CountsSingle in $DicCounts.GetEnumerator())

PowerShell中的哈希表也是如此,因此并不特别令人惊讶。

我认为它在这里崩溃了:

Foreach($Dic_CountsSingle in $Dic_Counts) 
该foreach循环需要一个数组作为其第二个参数$Dic_Counts是一个哈希表,因此它没有索引。现在我想知道一个有序哈希表是否可以在foreach循环中工作。它确实有一个索引

没有。Foreach也不会枚举有序哈希表。你得自己做。

我是这样做的:

$Dic_Counts.keys | %{ $Dic_Counts[$_]  }

好吧,我想知道PowerShell将自己迭代什么(在
foreach
或管道中)。它适用于
系统.Collections.Generic.List
,它当然不是一个数组,但也没有任何定义接口,它与数组有共同之处,也不是由字典或哈希表实现的。哇,这个语法是什么?“%”?@很可能是foreach对象的别名。您能解释一下为什么会这样吗?