Created
March 24, 2012 01:25
-
-
Save lazypower/2177037 to your computer and use it in GitHub Desktop.
Use reflection to copy existing properties from object to object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Reflection | |
Public Class ObjectCopy | |
''' <summary> | |
''' Copies an objects properties and fields from the source to destination | |
''' </summary> | |
''' <param name="newType">The BaseType you are copying from</param> | |
''' <param name="source">The source object copying FROM</param> | |
''' <param name="destination">the destination object copying TO</param> | |
''' <remarks></remarks> | |
Public Shared Sub ReflectionCopy(ByVal newType As Type, ByRef source As Object, ByRef destination As Object) | |
Dim myObjectFields As FieldInfo() = newType.GetFields( _ | |
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance) | |
For Each fi As FieldInfo In myObjectFields | |
Try | |
fi.SetValue(destination, fi.GetValue(source)) | |
Catch ex As Exception | |
'do nothing | |
End Try | |
Next | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment