如何创建PSObject对象的新克隆实例

时间:2022-05-10 19:51:30

I want to create new instance of my custom PSObject. I have a Button object created as PSObject and I want to create new object Button2 which has the same members as Button does, but I can't find a way how to clone the original object without making it referenced in original object (if I change a property in Button2 it changes in Button as well). Is there a way how to do it similarly as with hashtables and arrays via some Clone() method?

我想创建自定义PSObject的新实例。我有一个创建为PSObject的Button对象,我想创建新的对象Button2,它具有与Button相同的成员,但我找不到如何克隆原始对象而不在原始对象中引用它的方法(如果我更改Button2中的属性也会在Button中更改。有没有办法如何通过一些Clone()方法与哈希表和数组类似地做到这一点?

5 个解决方案

#1


9  

Indeed there is no clone method! However where there is a will...

确实没有克隆方法!但是如果有遗嘱......

$o = New-Object PsObject -Property @{ prop1='a' ; prop2='b' }
$o2 = New-Object PsObject
$o.psobject.properties | % {
    $o2 | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
}
$o.prop1 = 'newvalue'

$o
$o2

Output:

输出:

prop2     prop1                                                                 
-----     -----                                                                 
b         newvalue                                                              
b         a      

#2


52  

Easiest way is to use the Copy Method of a PsObject ==> $o2 = $o1.PsObject.Copy()

最简单的方法是使用PsObject的复制方法==> $ o2 = $ o1.PsObject.Copy()

$o1 = New-Object -TypeName PsObject -Property @{
    Fld1 = 'Fld1';
    Fld2 = 'Fld2';
    Fld3 = 'Fld3'}

$o2 = $o1.PsObject.Copy()

$o2 | Add-Member -MemberType NoteProperty -Name Fld4 -Value 'Fld4'
$o2.Fld1 = 'Changed_Fld'

$o1 | Format-List
$o2 | Format-List

Output:

输出:

Fld3 : Fld3
Fld2 : Fld2
Fld1 : Fld1

Fld3 : Fld3
Fld2 : Fld2
Fld1 : Changed_Fld
Fld4 : Fld4

#3


12  

Another possibility:

另一种可能性

 $o1 = New-Object PsObject -Property @{ prop1='a' ; prop2='b' }
 $o2 = $o1 | select *
 $o2.prop1 = 'newvalue'
 $o1.prop1
 $o2.prop1
 a
 newvalue

#4


6  

For some reason PSObject.Copy() doesn't work for all object types. Another solution to create a copy of an object is to convert it to/from Json then save it in a new variable:

由于某种原因,PSObject.Copy()不适用于所有对象类型。创建对象副本的另一个解决方案是将其转换为Json或从Json转换然后将其保存在新变量中:

$CustomObject1 = [pscustomobject]@{a=1; b=2; c=3; d=4}
$CustomObject2 = $CustomObject1 | ConvertTo-Json -depth 100 | ConvertFrom-Json
$CustomObject2 | add-Member -Name "e" -Value "5" -MemberType noteproperty

$CustomObject1 | Format-List
$CustomObject2 | Format-List

#5


0  

Put this in a Utility class or define it in your current section

将它放在Utility类中或在当前部分中定义它

function clone($obj)
{
    $newobj = New-Object PsObject
    $obj.psobject.Properties | % {Add-Member -MemberType NoteProperty -InputObject $newobj -Name $_.Name -Value $_.Value}
    return $newobj
}

Usage:

用法:

$clonedobj = clone $obj

$ clonedobj = clone $ obj

#1


9  

Indeed there is no clone method! However where there is a will...

确实没有克隆方法!但是如果有遗嘱......

$o = New-Object PsObject -Property @{ prop1='a' ; prop2='b' }
$o2 = New-Object PsObject
$o.psobject.properties | % {
    $o2 | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
}
$o.prop1 = 'newvalue'

$o
$o2

Output:

输出:

prop2     prop1                                                                 
-----     -----                                                                 
b         newvalue                                                              
b         a      

#2


52  

Easiest way is to use the Copy Method of a PsObject ==> $o2 = $o1.PsObject.Copy()

最简单的方法是使用PsObject的复制方法==> $ o2 = $ o1.PsObject.Copy()

$o1 = New-Object -TypeName PsObject -Property @{
    Fld1 = 'Fld1';
    Fld2 = 'Fld2';
    Fld3 = 'Fld3'}

$o2 = $o1.PsObject.Copy()

$o2 | Add-Member -MemberType NoteProperty -Name Fld4 -Value 'Fld4'
$o2.Fld1 = 'Changed_Fld'

$o1 | Format-List
$o2 | Format-List

Output:

输出:

Fld3 : Fld3
Fld2 : Fld2
Fld1 : Fld1

Fld3 : Fld3
Fld2 : Fld2
Fld1 : Changed_Fld
Fld4 : Fld4

#3


12  

Another possibility:

另一种可能性

 $o1 = New-Object PsObject -Property @{ prop1='a' ; prop2='b' }
 $o2 = $o1 | select *
 $o2.prop1 = 'newvalue'
 $o1.prop1
 $o2.prop1
 a
 newvalue

#4


6  

For some reason PSObject.Copy() doesn't work for all object types. Another solution to create a copy of an object is to convert it to/from Json then save it in a new variable:

由于某种原因,PSObject.Copy()不适用于所有对象类型。创建对象副本的另一个解决方案是将其转换为Json或从Json转换然后将其保存在新变量中:

$CustomObject1 = [pscustomobject]@{a=1; b=2; c=3; d=4}
$CustomObject2 = $CustomObject1 | ConvertTo-Json -depth 100 | ConvertFrom-Json
$CustomObject2 | add-Member -Name "e" -Value "5" -MemberType noteproperty

$CustomObject1 | Format-List
$CustomObject2 | Format-List

#5


0  

Put this in a Utility class or define it in your current section

将它放在Utility类中或在当前部分中定义它

function clone($obj)
{
    $newobj = New-Object PsObject
    $obj.psobject.Properties | % {Add-Member -MemberType NoteProperty -InputObject $newobj -Name $_.Name -Value $_.Value}
    return $newobj
}

Usage:

用法:

$clonedobj = clone $obj

$ clonedobj = clone $ obj