Tag Archives: Inheritance

Copy properties from base class to inherited class in copy constructor

When you want to inherit the properties of a base class in a derived class it’s handy to have a copy constructor that takes an instance of the base class as a parameter and copies the properties from the base class into the new class.

Usually you have to write a long list of source.property = me.property statements which is both tedious, typo prone, and must be rewritten every time either class changes.

Wouldn’t it be better if there was a single function you could call to do the bulk of the work for you?

Imports System.Reflection
''' <summary>
''' Library of copying functions
''' </summary>
''' <remarks>
''' Reference: http://stackoverflow.com/questions/1198886/c-sharp-using-reflection-to-copy-base-class-properties
''' </remarks>
Public NotInheritable Class Copier
''' <summary>
''' Generically copies the properties of one object to another.
''' </summary>
''' <typeparam name="T1">Source type</typeparam>
''' <typeparam name="T2">Target type</typeparam>
''' <param name="source">Source object</param>
''' <param name="target"></param>
''' <returns>Source object</returns>
''' <remarks></remarks>
Public Shared Function CopyFrom(Of T1 As Class, T2 As Class)(source As T1, target As T2) As T1

'Get the properties from each object
Dim srcFields As PropertyInfo() = target.[GetType]().GetProperties(BindingFlags.Instance Or BindingFlags.[Public] Or BindingFlags.GetProperty)
Dim destFields As PropertyInfo() = source.[GetType]().GetProperties(BindingFlags.Instance Or BindingFlags.[Public] Or BindingFlags.SetProperty)
'Copy all matches
For Each [property] In srcFields
Dim dest = destFields.FirstOrDefault(Function(x) x.Name = [property].Name)
If dest IsNot Nothing AndAlso dest.CanWrite Then
dest.SetValue(source, [property].GetValue(target, Nothing), Nothing)
End If

Return source

End Function

#Region "Example Usage"
''' <summary>
''' Example base class with a single property.
''' </summary>
''' <remarks></remarks>
Private Class SelfCopyingBase

Public Property Data As String

End Class
''' <summary>
''' Example derived class.
''' </summary>
''' <remarks></remarks>
Private Class SelfCopying
Inherits SelfCopyingBase
''' <summary>
''' Copy constructor.
''' </summary>
''' <param name="source"></param>
''' <remarks></remarks>
Public Sub New(source As SelfCopyingBase)

Copier.CopyFrom(Of SelfCopyingBase, SelfCopying)(source, Me)

End Sub

End Class

#End Region

End Class