powershell - Sync'd Hashtable 不是 PowerShell 显示友好的。尝试:[HashTable]::Synchronized(@{})

标签 powershell hashtable synchronized

我有一个来自 .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/

相关文章:

powershell - 有没有办法在 powershell session 中启用 "noninteractive"

c++ - c++ 中的哈希表能否以类似于 Python 中的字典的方式使用?

java - main 方法中的同步块(synchronized block)

powershell - Power Shell 测试连接,然后获取计算机名称或 mac-addr

file - 将文件从源目录复制到目标目录,并从指定目录中排除特定文件类型

powershell - 作为 TFS 构建的一部分,仅从 TFS checkin 文件

Java Hashtable.contains() 错误

javascript - 哈希表与 JavaScript 中的对象

java - 同步块(synchronized block)未锁定对象引用

java - 实例同步