将一个对象复制到另一个

时间:2011-02-04 21:37:24

标签: vb.net class reflection

.net 3.5,VS 2010 ...这是一个asp.net网站。

我有一个名为Agency的课程。有一个名为Agency_Queries的第二个类。 Agency_Queries inhert是Agency类。我正在尝试创建一个将代理中的类似属性复制到Agency_Queries的函数。我想出了如何做到这一点..但当我试图让它更通用,以便我可以传递我的类名和列表我做错了。

因此,如果有一个(代理商的)列表需要复制到(Agency_Queries)列表中,我会得到以下内容。

  Dim AgencyS As List(Of Agency) = Nothing
    Dim Oc As New Agency_Controller
    AgencyS = Oc.GetAgencyData(0)

    Dim AgencyQueriesS As New List(Of Agency_Queries)
    Dim _itemProperties() As Reflection.PropertyInfo = AgencyS.Item(0).GetType.GetProperties()
    For Each item In AgencyS
        Dim X As NewAgency_Queries
        _itemProperties = item.GetType().GetProperties()
        For Each p As Reflection.PropertyInfo In _itemProperties
            For Each s As Reflection.PropertyInfo In X.GetType().GetProperties()
                If p.Name = s.Name Then
                    s.SetValue(X, p.GetValue(item, Nothing), Nothing)
                End If
            Next
        Next
        AgencyQueriesS.Add(X)
    Next

问题是当我将Dim X作为新的Agency_Queries制作此通用时。我如何在一般意义上创建类的新实例。我需要将它作为一个新实例或每次添加到AgencyQueriesS列表时所有对象都具有相同的数据值。

这是通用版本......无效

 Shared Function CopyObject(ByVal inputList As Object, ByVal OutputClass As Object, ByVal outputList As Object) As Object
        Dim _itemProperties() As Reflection.PropertyInfo = inputList.Item(0).GetType.GetProperties()
        For Each item In inputList
            Dim oClean As Object = OutputClass
            For Each p As Reflection.PropertyInfo In _itemProperties
                For Each s As Reflection.PropertyInfo In oClean.GetType().GetProperties()
                    If p.Name = s.Name Then
                        s.SetValue(oClean, p.GetValue(item, Nothing), Nothing)
                    End If
                Next
            Next
            outputList.Add(oClean)
        Next
        Return outputList
    End Function

感谢 香农

2 个答案:

答案 0 :(得分:3)

我接受了一点这个,然后想出了这个:

    Imports System.Reflection

Public Class ObjectHelper

    ' Creates a copy of an object
    Public Shared Function GetCopy(Of SourceType As {Class, New})(ByVal Source As SourceType) As SourceType

        Dim ReturnValue As New SourceType
        Dim sourceProperties() As PropertyInfo = Source.GetType().GetProperties()

        For Each sourceProp As PropertyInfo In sourceProperties
            sourceProp.SetValue(ReturnValue, sourceProp.GetValue(Source, Nothing), Nothing)
        Next
        Return ReturnValue
    End Function
End Class

答案 1 :(得分:0)

我使用以下内容:

<Extension>
Public Sub CopyPropertiesByName(Of T1, T2)(dest As T1, src As T2)

  Dim srcProps = src.GetType().GetProperties()
  Dim destProps = dest.GetType().GetProperties()

  For Each loSrcProp In srcProps
    If loSrcProp.CanRead Then
      Dim loDestProp = destProps.FirstOrDefault(Function(x) x.Name = loSrcProp.Name)
      If loDestProp IsNot Nothing AndAlso loDestProp.CanWrite Then
        Dim loVal = loSrcProp.GetValue(src, Nothing)
        loDestProp.SetValue(dest, loVal, Nothing)
      End If
    End If
  Next
End Sub