开发者

Sync'd Hashtable not PowerShell display-friendly. Try: [HashTable]::Synchronized(@{})

开发者 https://www.devze.com 2023-01-20 20:23 出处:网络
I have an object coming from .Net that has a property of type SyncHashTable that can\'t be viewed w/o an exception being thrown.

I have an object coming from .Net that has a property of type SyncHashTable that can't be viewed w/o an exception being thrown.

One-line repro:

[HashTable]::Synchronized(@开发者_如何学Go{})

Multi-line easier to play with repro:

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

The error:

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

Anyone have any insight into this?


Got word back from Microsoft that this can be made to work:

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

Apparently it is a bug in the way the type is formatted for display.

0

精彩评论

暂无评论...
验证码 换一张
取 消