代码之家  ›  专栏  ›  技术社区  ›  rybosome

如何在VB.net中复制未知类型的对象?

  •  1
  • rybosome  · 技术社区  · 15 年前

    与其给出非常具体的案例(我之前做过),不如让我给出一个一般性的例子。假设我有一个函数,叫做callingFunction。它有一个参数,叫做parameter。参数的类型未知。然后让我们假设我希望复制这个参数,并将它作为一个新对象返回。例如,在伪代码中。。。

    Function callingFunction(ByVal parameter As Object) As Object
    
        Dim newObj As New Object
    
        'newObj has the same value as parameter, but is a distinctly different object
        'with a different reference
    
        newObj = parameter
    
        return newObj
    
    End Function
    

    编辑:附加信息

    我第一次发布这个问题时,只收到了一个回复——我觉得也许我把这个问题说得太具体了。我想我会解释更多,也许这会有帮助。我有一个ASP页面,上面有10个表。我正在尝试,使用VB代码,想出一个单一的解决方案来向任何表中添加新行。当用户单击一个按钮时,应该调用一个通用的“addrow”函数。

    我已经对它进行了彻底的测试,我的代码唯一失败的部分在于对象类型的动态生成。所以我问起复制对象的问题。顺便说一句,到目前为止发布的两个解决方案都不正确。感谢您迄今为止的帮助,也许这些额外的信息会让您更容易提供建议?

    3 回复  |  直到 15 年前
        1
  •  1
  •   Doc Brown    15 年前

    parameter 属于实现单例模式的类型。如果 是支持复制的类型,它应该实现 ICloneable 接口。你的函数可以是这样的:

    Function MyFunc(ByVal parameter As Object) As Object
        Dim cloneableObject As ICloneable = TryCast(parameter, ICloneable)
        If Not cloneableObject Is Nothing Then
            Return cloneableObject.Clone()
        Else
            Return Nothing
        End If
    End Function
    
        2
  •  1
  •   Bobby    15 年前

    您可以实现如下内容:

          Dim p1 As Person = New Person("Tim")
          Dim p2 As Object = CloneObject(p1)
          Dim sameRef As Boolean = p2 Is p1 'false'             
    
    
         Private Function CloneObject(ByVal o As Object) As Object
             Dim retObject As Object
             Try
                 Dim objType As Type = o.GetType
                 Dim properties() As Reflection.PropertyInfo = objType.GetProperties
                 retObject = objType.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, Nothing, o, Nothing)
                For Each propertyInfo As PropertyInfo In properties
                   If (propertyInfo.CanWrite) Then
                       propertyInfo.SetValue(retObject, propertyInfo.GetValue(o, Nothing), Nothing)
                   End If
                Next
             Catch ex As Exception
                retObject = o
             End Try
    
             Return retObject
           End Function
    
            Class Person
               Private _name As String
               Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                 _name = value
            End Set
              End Property
             Public Sub New()
             End Sub
             Public Sub New(ByVal name As String)
            Me.Name = name
             End Sub
     End Class
    
        3
  •  0
  •   Chris Dunaway    15 年前

    下面是一个适用于大多数对象的简单类(假设至少.Net 2.0):

    Public Class ObjectCloner
        Public Shared Function Clone(Of T)(ByVal obj As T) As T
            Using buffer As MemoryStream = New MemoryStream
                Dim formatter As New BinaryFormatter
                formatter.Serialize(buffer, obj)
                buffer.Position = 0
                Return DirectCast(formatter.Deserialize(buffer), T)
            End Using
        End Function
    End Class