one class property value to another class propety c#
public class PropertyCopier<TParent, TChild> where TParent : class
where TChild : class
{
public static void Copy(TParent parent, TChild child)
{
var parentProperties = parent.GetType().GetProperties();
var childProperties = child.GetType().GetProperties();
foreach (var parentProperty in parentProperties)
{
foreach (var childProperty in childProperties)
{
if (parentProperty.Name != "ID" && childProperty.Name != "ID" && parentProperty.Name == childProperty.Name && parentProperty.PropertyType == childProperty.PropertyType)
{
if (parentProperty.GetValue(parent) != null)
{
childProperty.SetValue(child, parentProperty.GetValue(parent));
}
break;
}
}
}
}
}
where TChild : class
{
public static void Copy(TParent parent, TChild child)
{
var parentProperties = parent.GetType().GetProperties();
var childProperties = child.GetType().GetProperties();
foreach (var parentProperty in parentProperties)
{
foreach (var childProperty in childProperties)
{
if (parentProperty.Name != "ID" && childProperty.Name != "ID" && parentProperty.Name == childProperty.Name && parentProperty.PropertyType == childProperty.PropertyType)
{
if (parentProperty.GetValue(parent) != null)
{
childProperty.SetValue(child, parentProperty.GetValue(parent));
}
break;
}
}
}
}
}
Comments
Post a Comment