Sync'd Hashtable does not support PowerShell. Try: [HashTable] :: Synchronized (@ {})

I have an object coming from .Net that has a property of type SyncHashTable that cannot be viewed without an exception being thrown.

Single line view:

[HashTable]::Synchronized(@{})

Multi-line is easier to play with playback:

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

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

Can anyone figure this out?

+3
source share
1 answer

Got a response 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, this is an error in the method of formatting the type to display.

+6
source

Source: https://habr.com/ru/post/1770565/


All Articles