Skip to content

Instantly share code, notes, and snippets.

@lazypower
Created March 24, 2012 01:25
Show Gist options
  • Save lazypower/2177037 to your computer and use it in GitHub Desktop.
Save lazypower/2177037 to your computer and use it in GitHub Desktop.
Use reflection to copy existing properties from object to object
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