copy one object to another

后端 未结 2 1727
悲&欢浪女
悲&欢浪女 2021-01-07 11:53

.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 Agen

2条回答
  •  再見小時候
    2021-01-07 12:26

    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
    

提交回复
热议问题