Beispiel #1
0
 public string CompileBinding(string expression, Type[] contexts, Type expectedType)
 {
     var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[]{
         new BindingPageInfoExtensionParameter(),
         }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray());
     for (int i = 1; i < contexts.Length; i++)
     {
         context = DataContextStack.Create(contexts[i], context);
     }
     var parser = new BindingExpressionBuilder();
     var parsedExpression = parser.ParseWithLambdaConversion(expression, context, BindingParserOptions.Create<ValueBindingExpression>(), expectedType);
     var expressionTree =
         TypeConversion.MagicLambdaConversion(parsedExpression, expectedType) ??
         TypeConversion.ImplicitConversion(parsedExpression, expectedType, true, true);
     var jsExpression = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService<JavascriptTranslator>().CompileToJavascript(expressionTree, context));
     jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
     JsTemporaryVariableResolver.ResolveVariables(jsExpression);
     return JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression);
 }