static Operator()
 {
     Convert  = ExpressionUtil.CreateExpression <TValue, TResult>(body => Expression.Convert(body, typeof(TResult)));
     Add      = ExpressionUtil.CreateExpression <TResult, TValue, TResult>(Expression.Add, true);
     Subtract = ExpressionUtil.CreateExpression <TResult, TValue, TResult>(Expression.Subtract, true);
     Multiply = ExpressionUtil.CreateExpression <TResult, TValue, TResult>(Expression.Multiply, true);
     Divide   = ExpressionUtil.CreateExpression <TResult, TValue, TResult>(Expression.Divide, true);
 }
Example #2
0
        static Operator()
        {
            Add      = ExpressionUtil.CreateExpression <T, T, T>(Expression.Add);
            Subtract = ExpressionUtil.CreateExpression <T, T, T>(Expression.Subtract);
            Divide   = ExpressionUtil.CreateExpression <T, T, T>(Expression.Divide);
            Multiply = ExpressionUtil.CreateExpression <T, T, T>(Expression.Multiply);

            GreaterThan        = ExpressionUtil.CreateExpression <T, T, bool>(Expression.GreaterThan);
            GreaterThanOrEqual = ExpressionUtil.CreateExpression <T, T, bool>(Expression.GreaterThanOrEqual);
            LessThan           = ExpressionUtil.CreateExpression <T, T, bool>(Expression.LessThan);
            LessThanOrEqual    = ExpressionUtil.CreateExpression <T, T, bool>(Expression.LessThanOrEqual);
            Equal    = ExpressionUtil.CreateExpression <T, T, bool>(Expression.Equal);
            NotEqual = ExpressionUtil.CreateExpression <T, T, bool>(Expression.NotEqual);

            Negate = ExpressionUtil.CreateExpression <T, T>(Expression.Negate);
            And    = ExpressionUtil.CreateExpression <T, T, T>(Expression.And);
            Or     = ExpressionUtil.CreateExpression <T, T, T>(Expression.Or);
            Not    = ExpressionUtil.CreateExpression <T, T>(Expression.Not);
            Xor    = ExpressionUtil.CreateExpression <T, T, T>(Expression.ExclusiveOr);

            Type typeT = typeof(T);

            if (typeT.IsValueType && typeT.IsGenericType && (typeT.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                // get the *inner* zero (not a null Nullable<TValue>, but default(TValue))
                Type nullType = typeT.GetGenericArguments()[0];
                Zero   = (T)Activator.CreateInstance(nullType);
                NullOp = (INullOp <T>)Activator.CreateInstance(typeof(StructNullOp <>).MakeGenericType(nullType));
            }
            else
            {
                Zero = default(T);
                if (typeT.IsValueType)
                {
                    NullOp = (INullOp <T>)Activator.CreateInstance(typeof(StructNullOp <>).MakeGenericType(typeT));
                }
                else
                {
                    NullOp = (INullOp <T>)Activator.CreateInstance(typeof(ClassNullOp <>).MakeGenericType(typeT));
                }
            }
        }