我想克隆一個字典到使用.clone()
方法稱爲$backUp
一個變量,但它與錯誤而失敗:複製PowerShell的字典到一個變量
Method invocation failed because [System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] does not contain a method named 'Clone'. At line:10 char:1 + $backUp = $originalKeyValuePairs.Clone() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
$originalKeyValuePairs = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$originalKeyValuePairs.add("key1", "value1")
$originalKeyValuePairs.add("key2", "value2")
$originalKeyValuePairs.add("key3", "value3")
$originalKeyValuePairs.add("key4", "value4")
Write-Output "This is the original dictionary" $originalKeyValuePairs
#Copy the "originalKeyValuePairs" into a variable
$backUp = $originalKeyValuePairs.Clone()
#update the 'key2' value to something else:
$originalKeyValuePairs["key2"] = "tempvalue"
#Now I'm done with updating the values. Now I want to restore my "$backUp" into the $originalKeyValuePairs
$originalKeyValuePairs = $backUp.clone()
Write-Output "Done with updating some of the value in original keyValuePairs and restore 'backUp' dictionary into 'originalKeyValuePairs'. Here is the unmodified dictionary" $originalKeyValuePairs
它的工作。非常感謝Nick。 – mahesh