// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Reflection; namespace E { public class A { public int Iv { get; set; } public string Sv { get; set; } } public class B { public decimal Mv { get; set; } public string Sv { get; set; } } public class Change { public string Property { get; set; } public object OldVal { get; set; } public object NewVal { get; set; } public static List Compare(T o1, T o2) { List res = new List(); foreach(PropertyInfo pi in typeof(T).GetProperties()) { if(!pi.GetValue(o1, null).Equals(pi.GetValue(o2, null))) { res.Add(new Change { Property=pi.Name, OldVal=pi.GetValue(o1, null), NewVal=pi.GetValue(o2, null) }); } } return res; } } public class Program { public static void Test(T o1, T o2) { List dif = Change.Compare(o1, o2); foreach(Change c in dif) { Console.WriteLine(c.Property + ": " + c.OldVal + " -> " + c.NewVal); } } public static void Main(string[] args) { A a1 = new A { Iv=123, Sv="ABC" }; A a2 = new A { Iv=124, Sv="ABC" }; Test(a1, a2); B b1 = new B { Mv=123.45m, Sv="ABC" }; B b2 = new B { Mv=123.45m, Sv="ABD" }; Test(b1, b2); } } }