Create a “clone” of this object, not point to it

2019-01-26 09:48发布

Let's say I got a list called

myFirstList

And then I want to create a copy of that list so I can do some tweaks of my own. So I do this:

mySecondList = myFirstList
mySecondList.doTweaks

But I noticed that the tweaks also affect the myFirstList object! I only want the tweaks to affect the second one...

And afterwards I will want to completely delete mySecondList, so I do mySecondList = Nothing and I'm good, right?

7条回答
ら.Afraid
2楼-- · 2019-01-26 10:10

Adam Rackis, I don't like your "Of course it does", because it is not at all obvious.

If you have a string variable that you assign to another string variabe, you do not change them both when making changes to one of them. They do not point to the same physical piece of memory, so why is it obvious that classes do?

Also, the thing is not even consistent. In the following case, you will have all elements in the array pointing at the same object (they all end up with the variable Number set to 10:

SourceObject = New SomeClass
For i = 1 To 10
   SourceObject.Number = i
   ObjectArray.Add = SourceObject   
Next i

BUT, the following will give you 10 different instances:

For i = 1 To 10
   SourceObject = New SomeClass
   SourceObject.Number = i
   ObjectArray.Add = SourceObject   
Next i

Apparently the scope of the object makes a difference, so it is not at all obvious what happens.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-26 10:15

But I noticed that the tweaks also affect the myFirstList object! I only want the tweaks to affect the second one...

Of course it does. Both variables are pointing to the same object in memory. Anything you do to the one, happens to the other.

You're going to need to do either a deep clone, or a shallow one, depending on your requirements. This article should give you a better idea what you need to do

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-26 10:18

Here is how you do it:

'copy one object to another via reflection properties
For Each p As System.Reflection.PropertyInfo In originalobject.GetType().GetProperties()
    If p.CanRead Then
        clone.GetType().GetProperty(p.Name).SetValue(clone, p.GetValue(OriginalObject, Nothing))
    End If
Next

in some cases when the clone object got read-only properties you need to check that first.

For Each p As System.Reflection.PropertyInfo In originalobject.GetType().GetProperties()
    If p.CanRead AndAlso clone.GetType().GetProperty(p.Name).CanWrite Then
        clone.GetType().GetProperty(p.Name).SetValue(clone, p.GetValue(OriginalObject, Nothing))
    End If
Next
查看更多
ら.Afraid
5楼-- · 2019-01-26 10:21

Expanding on Adam Rackies' answer I was able to implement the following code using VB.NET.

My goal was to copy a list of objects that served mainly as data transfer objects (i.e. database data). The first the class dtoNamedClass is defined and ShallowCopy method is added. A new variable named dtoNamedClassCloneVar is created and a LINQ select query is used to copy the object variable dtoNamedClassVar.

I was able to make changes to dtoNamedClassCloneVar without affecting dtoNamedClassVar.

Public Class dtoNamedClass


    ... Custom dto Property Definitions



  Public Function ShallowCopy() As dtoNamedClass
    Return DirectCast(Me.MemberwiseClone(), dtoNamedClass)
  End Function

End Class


Dim dtoNamedClassVar As List(Of dtoNamedClass) = {get your database data}

Dim dtoNamedClassCloneVar = 
    (From d In Me.dtoNamedClass
        Where {add clause if necessary}
        Select d.ShallowCopy()).ToList
查看更多
聊天终结者
6楼-- · 2019-01-26 10:22

clone is the object you are attempting to clone to.

dim clone as new YourObjectType

You declare it like that.

查看更多
Summer. ? 凉城
7楼-- · 2019-01-26 10:32

this works for me:

mySecondList = myFirstList.ToList
查看更多
登录 后发表回答