我有一个来自 .Net 的对象,它有一个 SyncHashTable 类型的属性,在没有抛出异常的情况下无法查看。
在线复现:
[HashTable]::Synchronized(@{})
多线更容易与 repro 一起玩:
$ht = new-object hashtable
$ht.add("foo", "bar")
$hts = [Hashtable]::Synchronized($ht)
$hts
错误:
format-default : Object reference not set to an instance of an object.
+ CategoryInfo : NotSpecified: (:) [format-default], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.FormatDefaultCommand
有人对此有任何见解吗?
最佳答案
从微软那里得到消息说这可以工作:
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
显然,这是为显示而格式化类型的方式中的错误。
关于powershell - Sync'd Hashtable 不是 PowerShell 显示友好的。尝试:[HashTable]::Synchronized(@{}),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3979801/