This method should do what you need:
public static void Apply<T>(T originalValuesObj, T newValuesObj, params string[] ignore)
where T : class
{
if (originalValuesObj == null || newValuesObj == null)
{
throw new ArgumentNullException(originalValuesObj == null ? "from" : "to");
}
Type type = typeof(T);
List<string> ignoreList = new List<string>(ignore ?? new string[] { });
foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (ignoreList.Contains(pi.Name))
{
continue;
}
object toValue = type.GetProperty(pi.Name).GetValue(newValuesObj, null);
type.GetProperty(pi.Name).SetValue(originalValuesObj, toValue, null);
}
return;
}
source
share