1

如果我有一个对象列表

$o1 = New-Object –TypeName PSObject –Property @{"MyKey"= "dog"; "MyValue"="Steve"}
$o2 = New-Object –TypeName PSObject –Property @{"MyKey"= "dog"; "MyValue"="Frank"}
$o3 = New-Object –TypeName PSObject –Property @{"MyKey"= "fish"; "MyValue"="Frank"}

$inputs = ($o1, $o2, $o3)

以及将它们分组到表格中然后显示它们的功能

Function Report
{
    param($records)
    $hashtable = $records | Group-object -AsHashTable -Property "MyKey" 
    Write-host "Format table:"
    $hashtable | format-table
    Write-host "Contains key:"
    $hashtable.Keys | %{ "Key '$_' found: " + $hashtable.ContainsKey($_) }
}

所以当我在我的输入列表上运行它时

Report($inputs)

我明白了

Name                           Value                                                                                                                             
----                           -----                                                                                                                             
dog                            {@{MyKey=dog; MyValue=Steve}, @{MyKey=dog; MyValue=Frank}}                                                                        
fish                           {@{MyKey=fish; MyValue=Frank}}                                                                                                    


Contains key:
Key 'dog' found: True
Key 'fish' found: True

正如我所料。

我现在可以运行我的列表了select-object

Report($inputs | select-object -Property MyKey, MyValue )

并从上面获得相同的输出Report

但是,如果我对值使用表达式MyKey

Report( $inputs | select-object -Property `
    @{ Label = "MyKey"; Expression = {$_.MyKey} },
    MyValue )

我得到一个看起来相同的分组表,但不允许我通过键访问值:

Name                           Value                                                                                                                             
----                           -----                                                                                                                             
dog                            {@{MyKey=dog; MyValue=Steve}, @{MyKey=dog; MyValue=Frank}}                                                                        
fish                           {@{MyKey=fish; MyValue=Frank}}                                                                                                    


Contains key:
Key 'dog' found: False
Key 'fish' found: False

为什么我不能通过键访问这个哈希表?


请注意,这个问题看起来很像这个问题,但该解决方案在这里不起作用。那是:

Add-Type -TypeDefinition @'
    public static class Helper {
        public static object IndexHashtableByPSObject(System.Collections.IDictionary table,object[] key) {
            return table[key[0]];
        }
    }
'@

Function Report
{
    param($records)
    $hashtable = $records | Group-object -AsHashTable -Property "MyKey" 
    $hashtable.Keys | %{ "Get using helper:" + [Helper]::IndexHashtableByPSObject($hashtable,$_) }
    $hashtable.Keys | %{ "Get using reflection:" + [Collections.IDictionary].InvokeMember('','GetProperty',$null,$hashtable,$_) }
}

返回

Get using helper:
Get using helper:
Get using reflection:
Get using reflection:

我实施这些解决方案是否错误?

4

0 回答 0