Exemple #1
0
        public override DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            Expression          boolExpr;
            BindingRestrictions rest;

            if (target.Value == null)
            {
                boolExpr = Expression.Constant(true);
                rest     = BindingRestrictions.GetExpressionRestriction(BinderHelper.IsNull(target.Expression));
            }
            else if (target.LimitType == typeof(bool))
            {
                boolExpr = Expression.Not(BinderHelper.Wrap(target.Expression, typeof(bool)));
                rest     = BindingRestrictions.GetTypeRestriction(target.Expression, typeof(bool));
            }
            else
            {
                boolExpr = Expression.Constant(false);
                rest     = BindingRestrictions.GetExpressionRestriction(
                    Expression.AndAlso(
                        BinderHelper.IsNotNull(target.Expression),
                        Expression.Not(Expression.TypeIs(target.Expression, typeof(bool)))));
            }
            var expr = BinderHelper.Wrap(boolExpr, this.ReturnType);

            return(new DynamicMetaObject(expr, rest));
        }
Exemple #2
0
        /// <summary>
        /// 左辺か右辺のいずれかがnullの場合の比較
        /// </summary>
        private DynamicMetaObject FallbackOnNull(DynamicMetaObject left, DynamicMetaObject right)
        {
            bool       value    = this.Operation == ExpressionType.Equal;
            Expression restExpr = null;

            if (left.Value == null && right.Value == null)
            {
                restExpr = Expression.And(
                    BinderHelper.IsNull(left.Expression),
                    BinderHelper.IsNull(right.Expression));
            }
            else if (left.Value == null)
            {
                value   ^= true;
                restExpr = Expression.AndAlso(
                    BinderHelper.IsNull(left.Expression),
                    BinderHelper.IsNotNull(right.Expression));
            }
            else
            {
                value   ^= true;
                restExpr = Expression.AndAlso(
                    BinderHelper.IsNull(right.Expression),
                    BinderHelper.IsNotNull(left.Expression));
            }
            var expr = BinderHelper.Wrap(Expression.Constant(value), this.ReturnType);
            var rest = BindingRestrictions.GetExpressionRestriction(restExpr);

            return(new DynamicMetaObject(expr, rest));
        }
Exemple #3
0
        public override DynamicMetaObject FallbackBinaryOperation(DynamicMetaObject left, DynamicMetaObject right, DynamicMetaObject errorSuggestion)
        {
            Expression          expr;
            BindingRestrictions rest;

            if (left.Value == null)
            {
                expr = BinderHelper.Wrap(right.Expression, this.ReturnType);
                rest = BindingRestrictions.GetExpressionRestriction(BinderHelper.IsNull(left.Expression));
            }
            else if (left.LimitType == typeof(bool))
            {
                expr = BinderHelper.Wrap(
                    Expression.Condition(BinderHelper.Wrap(left.Expression, typeof(bool)), left.Expression, right.Expression),
                    this.ReturnType);
                rest = BindingRestrictions.GetTypeRestriction(left.Expression, typeof(bool));
            }
            else
            {
                expr = BinderHelper.Wrap(left.Expression, this.ReturnType);
                rest = BindingRestrictions.GetExpressionRestriction(
                    Expression.AndAlso(
                        BinderHelper.IsNotNull(left.Expression),
                        Expression.Not(Expression.TypeIs(left.Expression, typeof(bool)))));
            }
            return(new DynamicMetaObject(expr, rest));
        }
        /// <summary>
        /// 左辺、右辺ともに、nullだった場合
        /// </summary>
        private DynamicMetaObject CompareBotheNull(DynamicMetaObject left, DynamicMetaObject right)
        {
            Contract.Requires <ArgumentException>(left.Value == null);
            Contract.Requires <ArgumentException>(right.Value == null);

            Expression value = null;

            switch (this.Operation)
            {
            case ExpressionType.LessThan:
            case ExpressionType.GreaterThan:
                value = Expression.Constant(false);
                break;

            case ExpressionType.LessThanOrEqual:
            case ExpressionType.GreaterThanOrEqual:
                value = Expression.Constant(true);
                break;
            }
            var expr = BinderHelper.Wrap(value, this.ReturnType);
            var rest = BindingRestrictions.GetExpressionRestriction(
                Expression.And(
                    BinderHelper.IsNull(left.Expression),
                    BinderHelper.IsNull(right.Expression)));

            return(new DynamicMetaObject(expr, rest));
        }
Exemple #5
0
        public override DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            const string errorMsg = "{0}を符号反転出来ません。";

            if (target.Value == null)
            {
                var msg      = String.Format(errorMsg, ConstantNames.NullText);
                var ctorInfo = typeof(InvalidOperationException).GetConstructor(new[] { typeof(string) });
                var expr     = Expression.Throw(Expression.New(ctorInfo, Expression.Constant(errorMsg)), this.ReturnType);
                var rest     = BindingRestrictions.GetExpressionRestriction(BinderHelper.IsNull(target.Expression));
                return(new DynamicMetaObject(expr, rest));
            }
            try {
                var expr = BinderHelper.Wrap(Expression.Negate(Expression.Convert(target.Expression, target.LimitType)), this.ReturnType);
                var rest = BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType);
                return(new DynamicMetaObject(expr, rest));
            }
            catch (InvalidOperationException) {
                var msgExpr = ExpressionHelper.BetaReduction <string, object, string>(
                    (format, obj) => String.Format(format, obj),
                    Expression.Constant(errorMsg), target.Expression);
                var ctorInfo = typeof(InvalidOperationException).GetConstructor(new[] { typeof(string) });
                var expr     = Expression.Throw(Expression.New(ctorInfo, msgExpr), this.ReturnType);
                var rest     = BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType);
                return(new DynamicMetaObject(expr, rest));
            }
        }
Exemple #6
0
 public override DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
 {
     if (target.Value == null)
     {
         return(new DynamicMetaObject(
                    BinderHelper.Wrap(Expression.Constant(true), this.ReturnType),
                    BindingRestrictions.GetExpressionRestriction(BinderHelper.IsNull(target.Expression))));
     }
     if (target.LimitType == typeof(bool))
     {
         return(new DynamicMetaObject(
                    BinderHelper.Wrap(Expression.Not(target.Expression), this.ReturnType),
                    BindingRestrictions.GetTypeRestriction(target.Expression, typeof(bool))));
     }
     return(new DynamicMetaObject(
                BinderHelper.Wrap(Expression.Constant(false), this.ReturnType),
                BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)));
 }
        /// <summary>
        /// 左辺、右辺のいずれかがnullで、もう一方はそうでない場合
        /// </summary>
        private DynamicMetaObject CompareEatherNull(DynamicMetaObject left, DynamicMetaObject right)
        {
            Contract.Requires <ArgumentException>(
                left.Value == null && right.Value != null ||
                left.Value != null && right.Value == null);

            bool?value = null;

            switch (this.Operation)
            {
            case ExpressionType.LessThan:
            case ExpressionType.LessThanOrEqual:
                value = false;
                break;

            case ExpressionType.GreaterThan:
            case ExpressionType.GreaterThanOrEqual:
                value = true;
                break;
            }
            Expression rest;
            bool       order;

            if (left.Value == null)
            {
                rest = Expression.AndAlso(
                    BinderHelper.IsNull(left.Expression),
                    BinderHelper.IsNotNull(right.Expression));
                order = true;
            }
            else
            {
                rest = Expression.AndAlso(
                    BinderHelper.IsNull(right.Expression),
                    BinderHelper.IsNotNull(left.Expression));
                order = false;
            }
            var expr = BinderHelper.Wrap(Expression.Constant(value ^ order), this.ReturnType);

            return(new DynamicMetaObject(expr, BindingRestrictions.GetExpressionRestriction(rest)));
        }