Follow-up from this thread.
An ordered hashtable cannot be cloned.
Is there an "easy" way to do this? I have indeed found some examples that seem overly complicated for such a "simple" task.
$a = [ordered]@{}
$b = $a.Clone()
Method invocation failed because [System.Collections.Specialized.OrderedDictionary] does not contain a method named 'Clone'.
OrderedDictionary do not contain Clone method (see also ICloneable interface). You have to do it manually:
$ordered = [ordered]@{a=1;b=2;c=3;d=4}
$ordered2 = [ordered]@{}
foreach ($pair in $ordered.GetEnumerator()) { $ordered2[$pair.Key] = $pair.Value }
While the answer given by Paweł Dyl does clone the ordered hash, it is not a Deep-Clone.
In order to do that, you need to do this:
# create a deep-clone of an object
$ms = New-Object System.IO.MemoryStream
$bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$bf.Serialize($ms, $ordered)
$ms.Position = 0
$clone = $bf.Deserialize($ms)
$ms.Close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With