copy one object to another

梦想的初衷 提交于 2020-01-11 04:13:08

问题


.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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/4903188/copy-one-object-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!