Question:
Follow-up from this thread.
Issue
An ordered hashtable cannot be cloned.
Question
Is there an “easy” way to do this? I have indeed found some examples that seem overly complicated for such a “simple” task.
MWE
1 2 3 |
$a = [ordered]@{} $b = $a.Clone() |
Output
Method invocation failed because [System.Collections.Specialized.OrderedDictionary] does not contain a method named 'Clone'.
Answer:
OrderedDictionary do not contain Clone method (see also ICloneable interface). You have to do it manually:
1 2 3 4 |
$ordered = [ordered]@{a=1;b=2;c=3;d=4} $ordered2 = [ordered]@{} foreach ($pair in $ordered.GetEnumerator()) { $ordered2[$pair.Key] = $pair.Value } |