public Expression <Func <T, int> > CreateGetHashCode() { var obj = Expression.Parameter(Type); var body = EqualityComparerHelper.CreateGetHashCodeBody(obj, MembersOrder); var lambda = Expression.Lambda <Func <T, int> >(body, obj); return(lambda); }
public Expression <Func <T, T, bool> > CreateEquals() { var x = Expression.Parameter(Type); var y = Expression.Parameter(Type); List <Expression> list = new List <Expression>(); var exitPoint = Expression.Label(typeof(bool)); var body = EqualityComparerHelper.CreateEqualsBody(x, y, CompareOptions, MembersOrder); var lambda = Expression.Lambda <Func <T, T, bool> >(body, x, y); return(lambda); }
public Expression <Func <IData, int> > CreateGetHashCodeMethod() { var obj = Expression.Parameter(typeof(IData)); var objValue = Expression.Variable(Type); var dataType = typeof(Data <>).MakeGenericType(Type); var body = Expression.Block(typeof(int), new ParameterExpression[] { objValue }, Expression.Assign(objValue, Expression.Convert(obj, dataType).Value()), EqualityComparerHelper.CreateGetHashCodeBody(objValue, MembersOrder) ); var lambda = Expression.Lambda <Func <IData, int> >(body, obj); return(lambda); }
public Expression <Func <IData, IData, bool> > CreateEqualsMethod() { var x = Expression.Parameter(typeof(IData)); var y = Expression.Parameter(typeof(IData)); var xValue = Expression.Variable(Type); var yValue = Expression.Variable(Type); var dataType = typeof(Data <>).MakeGenericType(Type); var body = Expression.Block(typeof(bool), new ParameterExpression[] { xValue, yValue }, Expression.Assign(xValue, Expression.Convert(x, dataType).Value()), Expression.Assign(yValue, Expression.Convert(y, dataType).Value()), EqualityComparerHelper.CreateEqualsBody(xValue, yValue, CompareOptions, MembersOrder) ); var lambda = Expression.Lambda <Func <IData, IData, bool> >(body, x, y); return(lambda); }
public static Expression CreateEqualsBody(Expression x, Expression y, CompareOption[] compareOptions, Func <Type, MemberInfo, int> membersOrder) { var type = x.Type; var exitPoint = Expression.Label(typeof(bool)); if (DataType.IsPrimitiveType(type) || type == typeof(Guid)) { return(EqualityComparerHelper.GetEqualsCommand(x, y, compareOptions[0], exitPoint, true)); } else { List <Expression> list = new List <Expression>(); int i = 0; int count = DataTypeUtils.GetPublicMembers(type, membersOrder).Count(); foreach (var member in DataTypeUtils.GetPublicMembers(type, membersOrder)) { list.Add(GetEqualsCommand(Expression.PropertyOrField(x, member.Name), Expression.PropertyOrField(y, member.Name), compareOptions[i++], exitPoint, i == count)); } return(Expression.Block(typeof(bool), list)); } }