Exemple #1
0
        /// <summary>
        /// 暗黙の型変換が存在する場合,それを利用する。
        /// </summary>
        private DynamicMetaObject TryImplicitCast(DynamicMetaObject left, DynamicMetaObject right)
        {
            Expression cmpExpr = null;

            if (BinderHelper.GetImplicitCast(left.LimitType, right.LimitType) != null)
            {
                try {
                    cmpExpr = Expression.MakeBinary(this.Operation,
                                                    BinderHelper.Wrap(left.Expression, left.LimitType, right.LimitType),
                                                    BinderHelper.Wrap(right.Expression, right.LimitType));
                }
                catch (InvalidCastException) { }
            }
            if (BinderHelper.GetImplicitCast(right.LimitType, left.LimitType) != null)
            {
                try {
                    cmpExpr = Expression.MakeBinary(this.Operation,
                                                    BinderHelper.Wrap(left.Expression, left.LimitType),
                                                    BinderHelper.Wrap(right.Expression, right.LimitType, left.LimitType));
                }
                catch (InvalidCastException) { }
            }
            if (cmpExpr == null)
            {
                return(null);
            }
            var expr = BinderHelper.Wrap(cmpExpr, this.ReturnType);
            var rest = BinderHelper.GetTypeRestriction(left, right);

            return(new DynamicMetaObject(expr, rest));
        }
        /// <summary>
        /// 暗黙のキャストを探す。
        /// </summary>
        private DynamicMetaObject DefferentType(DynamicMetaObject left, DynamicMetaObject right)
        {
            var val = TryCalcNumeric(left, right);

            if (val != null)
            {
                return(val);
            }
            // 左辺のキャスト
            var mInfo = BinderHelper.GetImplicitCast(left.LimitType, right.LimitType);

            if (mInfo != null)
            {
                Expression expr = Expression.MakeBinary(this.Operation,
                                                        BinderHelper.Wrap(left.Expression, left.LimitType, right.LimitType),
                                                        Expression.Convert(right.Expression, right.LimitType));
                return(new DynamicMetaObject(
                           BinderHelper.Wrap(expr, this.ReturnType),
                           BinderHelper.GetTypeRestriction(left, right)));
            }
            // 右辺のキャスト
            mInfo = BinderHelper.GetImplicitCast(right.LimitType, left.LimitType);
            if (mInfo != null)
            {
                Expression expr = Expression.MakeBinary(this.Operation,
                                                        Expression.Convert(left.Expression, left.LimitType),
                                                        BinderHelper.Wrap(right.Expression, right.LimitType, left.LimitType));
                return(new DynamicMetaObject(
                           BinderHelper.Wrap(expr, this.ReturnType),
                           BinderHelper.GetTypeRestriction(left, right)));
            }
            return(BinderHelper.NoResult(_name, this.ReturnType, left, right));
        }