using System; using System.Linq; using System.Linq.Expressions; public class Test { public static Func MakeComparator() { var lhs = Expression.Parameter(typeof (T), "lhs"); var rhs = Expression.Parameter(typeof (T), "rhs"); var allPropChecks = typeof(T) .GetProperties() .Where(p => p.CanRead && p.GetIndexParameters().Length == 0) .Select(p => Expression.Equal(Expression.Property(lhs, p), Expression.Property(rhs, p))) .ToList(); Expression compare; if (allPropChecks.Count == 0) { return (a,b) => true; // Objects with no properties are the same } else { compare = allPropChecks[0]; compare = allPropChecks .Skip(1) .Aggregate(compare, Expression.AndAlso); } return (Func)Expression.Lambda(compare, new[] { lhs, rhs }).Compile(); } class Point3D { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } } public static void Main() { var cmp = MakeComparator(); var p1 = new Point3D {X = 1, Y = 2, Z = 3}; var p2 = new Point3D { X = 1, Y = 2, Z = 3 }; var p3 = new Point3D { X = 1, Y = 3, Z = 1 }; Console.WriteLine(cmp(p1, p2)); Console.WriteLine(cmp(p2, p3)); } }