Created
December 7, 2018 19:59
-
-
Save holyqqwqqasd/26cac25c928e49fe7c2ca60532ae09ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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