Skip to content

Instantly share code, notes, and snippets.

@lluisfranco
lluisfranco / ClassExtensions01.cs
Last active April 14, 2023 11:30
Map - Copy base class properties to derived class
public static TT Map<TT, ST>(this TT target, ST source) where TT : class
{
var tprops = typeof(TT).GetProperties().Where(x => x.CanWrite).ToList();
tprops.Where(x => x.CanWrite).ToList().ForEach(prop =>
{
var sp = source.GetType().GetProperty(prop.Name);
var tp = target.GetType().GetProperty(prop.Name);
if (sp != null && tp != null)
{
var value = sp.GetValue(source, null);