using static System.Console;
public static void Main()
var result = m1.Difference(m2);
public static class ReflectionExtensions
public static TModel Difference<TModel>(this TModel model1, TModel model2) where TModel : new()
static bool AreEqual(object obj1, object obj2) =>
(obj1 is null && obj2 is null) ||
Type type = typeof(TModel);
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
TModel result = properties.Aggregate(new TModel(), (seed, property) =>
var value1 = property.GetValue(model1);
var value2 = property.GetValue(model2);
if (!AreEqual(value1, value2))
property.SetValue(seed, value1);
public string FieldA { get; set; }
public string FieldB { get; set; }
public string FieldC { get; set; }
public int FieldD { get; set; }
public string FieldE { get; set; }
public override string ToString()
return $"{{ A = {FieldA}, B = {FieldB}, C = {FieldC}, D = {FieldD}, E = {FieldE} }}";