public ParsedExpressionBindingProperty GetExpression(OriginalStringBindingProperty originalString, DataContextStack dataContext, BindingParserOptions options, ExpectedTypeBindingProperty expectedType = null) { var expr = bindingParser.ParseWithLambdaConversion(originalString.Code, dataContext, options, expectedType?.Type ?? typeof(object)); if (expr is StaticClassIdentifierExpression) { throw new Exception($"'{originalString.Code}' is a static class reference, not a valid expression."); } else if (expr is UnknownStaticClassIdentifierExpression) { expr = expr.Reduce(); } return(new ParsedExpressionBindingProperty(expr)); }
public CastedExpressionBindingProperty ConvertExpressionToType(ParsedExpressionBindingProperty expr, ExpectedTypeBindingProperty expectedType = null) { var destType = expectedType?.Type ?? typeof(object); var convertedExpr = TypeConversion.ImplicitConversion(expr.Expression, destType, throwException: false, allowToString: true); return(new CastedExpressionBindingProperty( // if the expression is of type object (i.e. null literal) try the lambda conversion. convertedExpr != null && expr.Expression.Type != typeof(object) ? convertedExpr : TypeConversion.MagicLambdaConversion(expr.Expression, destType) ?? TypeConversion.ImplicitConversion(expr.Expression, destType, throwException: true, allowToString: true) )); }
public CastedExpressionBindingProperty ConvertExpressionToType(ParsedExpressionBindingProperty expr, ExpectedTypeBindingProperty expectedType = null) => new CastedExpressionBindingProperty(TypeConversion.ImplicitConversion(expr.Expression, expectedType?.Type ?? typeof(object), throwException: true, allowToString: true));
public CommandJavascriptBindingProperty CreateJs(IdBindingProperty id, ExpectedTypeBindingProperty expectedType = null) => new CommandJavascriptBindingProperty(CreateJsPostbackInvocation( id.Id, needsCommandArgs: expectedType?.Type?.GetDelegateArguments()?.Length.Apply(len => len != 0) ));
public CastedExpressionBindingProperty ConvertExpressionToType(ParsedExpressionBindingProperty exprP, ExpectedTypeBindingProperty expectedTypeP = null) { var expr = exprP.Expression; var expectedType = expectedTypeP?.Type ?? typeof(object); if (expectedType == typeof(object)) { expectedType = typeof(Command); } if (!typeof(Delegate).IsAssignableFrom(expectedType)) { throw new Exception($"Command bindings must be assigned to properties of Delegate type, not { expectedType }"); } var normalConvert = TypeConversion.ImplicitConversion(expr, expectedType); if (normalConvert != null && expr.Type != typeof(object)) { return(new CastedExpressionBindingProperty(normalConvert)); } // wrap expression into a lambda if (typeof(Delegate).IsAssignableFrom(expectedType) && !typeof(Delegate).IsAssignableFrom(expr.Type)) { var invokeMethod = expectedType.GetMethod("Invoke"); expr = TaskConversion(expr, invokeMethod.ReturnType); return(new CastedExpressionBindingProperty(Expression.Lambda( expectedType, expr, invokeMethod.GetParameters().Select(p => Expression.Parameter(p.ParameterType, p.Name)) ))); } // TODO: convert delegates to another delegates throw new Exception($"Can not convert expression '{ expr }' to '{expectedType}'."); }