private Expression ConvertIfNeeded(Expression expression, Type type) { if (expression.Type != type) { return(Expression.Dynamic( OldConvertToAction.Make(Binder, type, ConversionResultKind.ExplicitCast), type, _rule.Context, expression )); } return(expression); }
/// <summary> /// Helper that checkes both types to see if either one defines the specified conversion /// method. /// </summary> private MetaObject TryOneConversion(ConversionResultKind kind, Type toType, Type type, Type fromType, string methodName, bool isImplicit, Restrictions restrictions, MetaObject arg) { OldConvertToAction action = OldConvertToAction.Make(this, toType, kind); MemberGroup conversions = GetMember(action, fromType, methodName); MetaObject res = TryUserDefinedConversion(kind, toType, type, conversions, isImplicit, restrictions, arg); if (res != null) { return(res); } // then on the type we're trying to convert to conversions = GetMember(action, toType, methodName); return(TryUserDefinedConversion(kind, toType, type, conversions, isImplicit, restrictions, arg)); }
/// <summary> /// Converts the provided expression to the given type. The expression is safe to evaluate multiple times. /// </summary> public virtual Expression ConvertExpression(Expression expr, Type toType, ConversionResultKind kind, Expression context) { ContractUtils.RequiresNotNull(expr, "expr"); ContractUtils.RequiresNotNull(toType, "toType"); Type exprType = expr.Type; if (toType == typeof(object)) { if (exprType.IsValueType) { return(Expression.Convert(expr, toType)); } else { return(expr); } } if (toType.IsAssignableFrom(exprType)) { return(expr); } Type visType = CompilerHelpers.GetVisibleType(toType); Expression[] args; if (context != null) { args = new Expression[] { context, expr }; } else { args = new Expression[] { expr }; } return(Expression.Dynamic( OldConvertToAction.Make(this, visType, kind), visType, args )); }