.net - Deep copying a PSObject -
i have powershell script in following
$somepsobjecthashtables = new-object hashtable[] $somepsobject.length; $somepsobjects = import-csv $csvpath 0..($somepsobject.length - 1) | foreach-object { $i = $_; $somepsobjecthashtables[$i] = @{}; $somepsobject[$_].psobject.properties | foreach-object { $somepsobjecthashtables[$i][$_.name] = $_.value; } }
i need because want make several distinct copies of data in csv perform several distinct manipulations. in sense i'm performing "inner join" on resulting array of psobject. can iterate through $somepsobjecthashtables
foreach-object , call hashtable.clone() on each member of array. can use new-object psobject -property $somehashtable[$i]
deep copy of psobject.
my question is, there easier way of making deep copy, without intermediary hashtable?
for getting deep copies can use binary serialization (assuming data serializable; case data come csv):
# original data $data = import-csv ... # serialize , deserialize data using binaryformatter $ms = new-object system.io.memorystream $bf = new-object system.runtime.serialization.formatters.binary.binaryformatter $bf.serialize($ms, $data) $ms.position = 0 $data2 = $bf.deserialize($ms) $ms.close() # use deep copied data $data2
Comments
Post a Comment