2010-10-20 27 views
3

J'ai un objet provenant de .Net qui a une propriété de type SyncHashTable qui ne peut pas être affichée sans qu'une exception soit levée.Sync'd Hashtable n'est pas compatible PowerShell. Essayez: [HashTable] :: Synchronized (@ {})

repro une ligne:

[HashTable]::Synchronized(@{}) 

multi-ligne plus facile à jouer avec repro:

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

L'erreur:

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

Quelqu'un a une idée de ce?

Répondre

6

Got mot retour de Microsoft que cela peut être fait pour travailler:

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 

Apparemment, il est un bogue dans la façon dont le type est formaté pour l'affichage.

+1

BTW, si vous voulez, votez sur le bogue ici - https://connect.microsoft.com/PowerShell/feedback/details/615449/display-of-synchronized-hashtable-generates-error –