Skip to content

Instantly share code, notes, and snippets.

@holyqqwqqasd
Created December 7, 2018 19:59
Show Gist options
  • Save holyqqwqqasd/26cac25c928e49fe7c2ca60532ae09ee to your computer and use it in GitHub Desktop.
Save holyqqwqqasd/26cac25c928e49fe7c2ca60532ae09ee to your computer and use it in GitHub Desktop.
private static IEqualityComparer<T> GetInstance()
{
var type = typeof(T);
ParameterExpression[] parameters =
{
Expression.Parameter(type, "x"),
Expression.Parameter(type, "y")
};
var result = type.GetProperties().Aggregate<PropertyInfo, Expression>(
Expression.Constant(true),
(acc, prop) =>
Expression.And(acc,
Expression.Equal(
Expression.Property(parameters[0], prop.Name),
Expression.Property(parameters[1], prop.Name))));
var expr = Expression.Lambda<Func<T, T, bool>>(result, parameters);
var equalFunc = expr.Compile();
return new DelegateEqualityComparer(equalFunc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment