Esempio n. 1
0
        // The 'rightParameter' argument is not used but by having the same signature for both methods allows for a few optimizations in other places.
        public static Expression CreateGetHashCodeExpression <TProperty>(
            PropertyInfo property,
            AutoEqualityPropertyAttribute attribute,
            Expression leftParameter,
            Expression rightParameter)
        {
            if (property.PropertyType == typeof(string) || property.PropertyType.IsEnum)
            {
                // Short-cut to have compiler write this expression for us.
                var getHashCodeFunc = (Expression <Func <TProperty, int> >)((obj) => GetComparer <TProperty>(attribute).GetHashCode(obj));

                return(Expression.Invoke(
                           getHashCodeFunc,
                           Expression.Property(leftParameter, property)
                           ));
            }

            // Call the instance 'GetHashCode' method by default.

            var getHashCodeMethod = property.PropertyType.GetMethod(nameof(GetHashCode));

            // ReSharper disable once AssignNullToNotNullAttribute - getHashCodeMethod is never null
            return(Expression.Call(
                       Expression.Property(leftParameter, property),
                       getHashCodeMethod
                       ));
        }
Esempio n. 2
0
        private static IEqualityComparer <TProperty> GetComparer <TProperty>(AutoEqualityPropertyAttribute attribute)
        {
            if (typeof(TProperty) == typeof(string))
            {
                return((IEqualityComparer <TProperty>)StringComparers[attribute.StringComparison]);
            }

            return(EqualityComparer <TProperty> .Default);
        }
Esempio n. 3
0
        public static Expression CreateEqualsExpression <TProperty>(
            PropertyInfo property,
            AutoEqualityPropertyAttribute attribute,
            Expression leftParameter,
            Expression rightParameter)
        {
            if (property.PropertyType == typeof(string) || property.PropertyType.IsEnum)
            {
                // Short-cut to have compiler write this expression for us.
                var equalsFunc = (Expression <Func <TProperty, TProperty, bool> >)((x, y) => GetComparer <TProperty>(attribute).Equals(x, y));

                return(Expression.Invoke(
                           equalsFunc,
                           Expression.Property(leftParameter, property),
                           Expression.Property(rightParameter, property)
                           ));
            }

            var equalsMethod = GetEqualsMethod(property.PropertyType);

            return(Expression.Call(Expression.Property(leftParameter, property), equalsMethod, Expression.Property(rightParameter, property)));
        }