vb.net 创建此对象的“克隆”,而不是指向它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5574024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a "clone" of this object, not point to it
提问by Voldemort
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...
但我注意到调整也会影响myFirstList
对象!我只希望调整影响第二个......
And afterwards I will want to completely delete mySecondList
, so I do mySecondList = Nothing
and I'm good, right?
之后我会想要完全删除mySecondList
,所以我这样做了mySecondList = Nothing
,我很好,对吧?
采纳答案by Adam Rackis
But I noticed that the tweaks also affect the myFirstList object! I only want the tweaks to affect the second one...
但我注意到这些调整也会影响 myFirstList 对象!我只希望调整影响第二个......
Of course it does. Both variables are pointing to the same objectin 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 articleshould give you a better idea what you need to do
您将需要进行深度克隆或浅层克隆,具体取决于您的要求。 这篇文章应该让你更好地了解你需要做什么
回答by FrankB
Adam Rackis, I don't like your "Of course it does", because it is not at all obvious.
Adam Rackis,我不喜欢你的“当然可以”,因为它一点也不明显。
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:
此外,事情甚至不一致。在以下情况下,数组中的所有元素都将指向同一个对象(它们最终都将变量 Number 设置为 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:
但是,以下将为您提供 10 个不同的实例:
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.
显然,对象的范围有所不同,所以发生了什么并不明显。
回答by Bala R
Since you have not divulged the type of item that you are storing n your list, I assume it's something that's implementing IClonable (Otherwise, if you can, implement IClonable, or figure out a way to clone individual item in the list).
由于您没有透露您在列表中存储的项目类型,我假设它正在实现 IClonable(否则,如果可以,请实现 IClonable,或者找出一种方法来克隆列表中的单个项目)。
Try something like this
尝试这样的事情
mySecondList = myFirstList.[Select](Function(i) i.Clone()).ToList()
回答by deuan
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
回答by wavedrop
Expanding on Adam Rackies' answer I was able to implement the following code using VB.NET.
扩展Adam Rackies的回答,我能够使用 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 dtoNamedClassis defined and ShallowCopymethod is added. A new variable named dtoNamedClassCloneVaris created and a LINQ select query is used to copy the object variable dtoNamedClassVar.
我的目标是复制一个主要用作数据传输对象(即数据库数据)的对象列表。首先定义了类dtoNamedClass并添加了ShallowCopy方法。创建了一个名为dtoNamedClassCloneVar的新变量,并使用 LINQ 选择查询来复制对象变量 dtoNamedClassVar。
I was able to make changes to dtoNamedClassCloneVarwithout affecting dtoNamedClassVar.
我能够更改dtoNamedClassCloneVar不影响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
回答by myQwil
this works for me:
这对我有用:
mySecondList = myFirstList.ToList
回答by user8005205
clone is the object you are attempting to clone to.
clone 是您尝试克隆到的对象。
dim clone as new YourObjectType
You declare it like that.
你这样声明。