.NET Tip : How to copy all property values from One class to another?
Do you ever wanted to copy ALL Property Values from one .net Class to another (Whilst Both of them having properties with similar names)?
Heres Method for that :
public static void CopyPropertyValues(object source, object destination)
{
Type sourceType = source.GetType();
Type destinationType = destination.GetType();
foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
{
PropertyInfo destinationProperty = destinationType.GetProperty(sourceProperty.Name);
if (destinationProperty != null)
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
}
}