예제 #1
0
        public override Expression /*!*/ ConvertExpression(Expression /*!*/ expr, ParameterInfo info, Type /*!*/ toType)
        {
            Type fromType = expr.Type;

            // block:
            if (fromType == typeof(MissingBlockParam))
            {
                Debug.Assert(toType == typeof(BlockParam) || toType == typeof(MissingBlockParam));
                return(Ast.Constant(null));
            }

            if (fromType == typeof(BlockParam) && toType == typeof(MissingBlockParam))
            {
                return(Ast.Constant(null));
            }

            // protocol conversions:
            if (info != null && info.IsDefined(typeof(DefaultProtocolAttribute), false))
            {
                var action = ProtocolConversionAction.TryGetConversionAction(toType);
                if (action != null)
                {
                    // TODO: once we work with MetaObjects, we could inline these dynamic sites:
                    return(Ast.Dynamic(action, toType, ScopeExpression, expr));
                }

                throw new InvalidOperationException(String.Format("No default protocol conversion for type {0}.", toType));
            }

            return(Binder.ConvertExpression(expr, toType, ConversionResultKind.ExplicitCast, ScopeExpression));
        }
예제 #2
0
        public override DynamicMetaObject /*!*/ BindConvert(ConvertBinder /*!*/ binder)
        {
            var protocolConversion = ProtocolConversionAction.TryGetDefaultConversionAction(Context, binder.Type);

            if (protocolConversion != null)
            {
                return(protocolConversion.Bind(this, DynamicMetaObject.EmptyMetaObjects));
            }
            else
            {
                return(binder.FallbackConvert(this));
            }
        }