Sync'd Hashtable不是PowerShell显示友好的.尝试:[HashTable] ::同步(@ {})

xcu*_*cud 3 powershell hashtable synchronized

我有一个来自.Net的对象,它具有SyncHashTable类型的属性,在抛出异常时无法查看.

单行复制:

[HashTable]::Synchronized(@{})
Run Code Online (Sandbox Code Playgroud)

多行更容易玩repro:

$ht = new-object hashtable
$ht.add("foo", "bar")
$hts = [Hashtable]::Synchronized($ht)
$hts
Run Code Online (Sandbox Code Playgroud)

错误:

format-default : Object reference not set to an instance of an object.
    + CategoryInfo          : NotSpecified: (:) [format-default], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.FormatDefaultCommand
Run Code Online (Sandbox Code Playgroud)

有人对此有任何见解吗?

Kei*_*ill 6

从微软那里得到回复,说这可以工作:

PS> $r = [hashtable]::Synchronized(@{})
PS> $r|format-table -expand coreonly -autoSize

Count IsReadOnly IsFixedSize IsSynchronized SyncRoot      Keys Values
----- ---------- ----------- -------------- --------      ---- ------
    0      False       False           True System.Object {}   {}

PS> $r.Add("key","value")
PS> $r["key"]
value
Run Code Online (Sandbox Code Playgroud)

显然,这是格式化显示方式的错误.