views:

28

answers:

1

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(@{})

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?

+2  A: 

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.

Keith Hill
BTW, if you want, vote on the bug here - https://connect.microsoft.com/PowerShell/feedback/details/615449/display-of-synchronized-hashtable-generates-error
Keith Hill