copy one object to another

2019-02-15 23:19发布

.net 3.5, VS 2010... this is for an asp.net website.

I have an class called Agency. there is a second class called Agency_Queries. Agency_Queries inhertis the Agency class. I'm trying to create a function that will copy the like properties in Agency to Agency_Queries. I figured out how to do that.. but when i try to make it more generic so that i can pass in my class name and lists i'm doing something wrong.

So if there is an list(of Agency) that needs to be copied to list(of Agency_Queries) i've got something like the following.

  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

the problem is when i go to make this generic by the Dim X as new Agency_Queries. How do i go about creating a new instance of the class in a generic sense. I need to have it be a new instance or each time it gets added to teh AgencyQueriesS list all the objects have the same data values.

Here is the generic version... not working

 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

thanks shannon

2条回答
太酷不给撩
2楼-- · 2019-02-15 23:38

I took a little of this and that and came up with this:

    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
查看更多
smile是对你的礼貌
3楼-- · 2019-02-16 00:01

I use the following:

<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
查看更多
登录 后发表回答