创建对象的深层副本

这个问题在这里已经有了答案:

  • 深入克隆对象39个答案

  • Public Sub New(ByVal p_Name As String, ByVal p_Unit As String, ByVal p_Value As Object, ByVal p_Type As String)
      m_Name = p_Name
      m_Unit = p_Unit
      m_Type = p_Type
      If TypeOf (p_Value) Is Double Then
        m_Value = p_Value
      ElseIf TypeOf (p_Value) Is clsVectorParameter Then
        m_Value = p_Value.Clone()
      End If
    End Sub
    

    为了让代码与例如创建XElement的克隆保持一致,请从构造函数中执行此操作:

    Dim obj1 = new clsVectorParameter(1, 1, 1)
    Dim obj2 = new clsVectorParameter(obj1)
    

    所以现在你只需要编写一个重载的构造函数,并且不需要接口或单独的函数。

    一个重载的构造函数可以这样做:(你需要调整它到你自己的类):

    Public Class Foo
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer
    
    Sub New(a As Integer, b As Integer, c As Integer)
        x = a
        y = b
        z = c
    End Sub
    
    Sub New(old As Foo)
        x = old.x
        y = old.y
        z = old.z
    End Sub
    End Class
    
    链接地址: http://www.djcxy.com/p/40775.html

    上一篇: Create deep copy of object

    下一篇: Is there an Object1.CopyTo(Object2) method in .NET?