Exemple #1
0
        /// <summary>
        ///     Dispatches the expression.
        /// </summary>
        /// <param name="node">The expression to visit.</param>
        /// <returns>The modified expression, if it or any subexpression was modified; otherwise, returns the original expression.</returns>
        public IExpressionNode Visit(IExpressionNode node)
        {
            var    nodes      = new List <IExpressionNode>();
            var    members    = new List <string>();
            string memberName = node.TryGetMemberName(true, true, nodes, members);

            if (memberName == null)
            {
                var relativeExp = nodes[0] as IRelativeSourceExpressionNode;
                if (relativeExp != null)
                {
                    relativeExp.MergePath(string.Join(".", members));
                    return(relativeExp);
                }

                var methodCall = nodes[0] as MethodCallExpressionNode;
                if (methodCall != null && methodCall.Target is ResourceExpressionNode)
                {
                    if (RelativeSourceAliases.Contains(methodCall.Method))
                    {
                        if ((methodCall.Arguments.Count == 1 || methodCall.Arguments.Count == 2) && methodCall.Arguments[0] is IMemberExpressionNode)
                        {
                            int level        = 1;
                            var relativeType = (IMemberExpressionNode)methodCall.Arguments[0];
                            if (methodCall.Arguments.Count == 2)
                            {
                                level = (int)((IConstantExpressionNode)methodCall.Arguments[1]).Value;
                            }
                            return(new RelativeSourceExpressionNode(relativeType.Member, (uint)level, string.Join(".", members)));
                        }
                    }

                    if (ElementSourceAliases.Contains(methodCall.Method))
                    {
                        if (methodCall.Arguments.Count == 1 && methodCall.Arguments[0] is IMemberExpressionNode)
                        {
                            var elementSource = (IMemberExpressionNode)methodCall.Arguments[0];
                            return(new RelativeSourceExpressionNode(elementSource.Member, string.Join(".", members)));
                        }
                    }
                }
            }
            return(node);
        }
Exemple #2
0
        public IExpressionNode Visit(IExpressionNode node)
        {
            var member = node as IMemberExpressionNode;

            if (member != null && member.Target is ResourceExpressionNode)
            {
                //$self, $this --> $BindingServiceProvider.ResourceResolver.SelfResourceName
                if (member.Member == "self" || member.Member == "this")
                {
                    return(new MemberExpressionNode(member.Target, BindingServiceProvider.ResourceResolver.SelfResourceName));
                }
                //$context --> $BindingServiceProvider.ResourceResolver.DataContextResourceName
                if (member.Member == "context")
                {
                    return(new MemberExpressionNode(member.Target, BindingServiceProvider.ResourceResolver.DataContextResourceName));
                }
                //$args, $arg --> $GetEventArgs()
                if (member.Member == "args" || member.Member == "arg")
                {
                    return(new MethodCallExpressionNode(member.Target, DefaultBindingParserHandler.GetEventArgsMethod, Empty.Array <IExpressionNode>(), Empty.Array <string>()));
                }
            }

            var methodCallExp = node as IMethodCallExpressionNode;

            if (methodCallExp != null && methodCallExp.Target is ResourceExpressionNode)
            {
                //$Format() --> string.Format()
                if (methodCallExp.Method == "Format")
                {
                    return(new MethodCallExpressionNode(new ConstantExpressionNode(typeof(string)), methodCallExp.Method, methodCallExp.Arguments, methodCallExp.TypeArgs));
                }
                //$OneTime(Expression) --> oneTimeImpl.GetValue(() => Expression)
                if (methodCallExp.Method == "OneTime" && methodCallExp.Arguments.Count == 1)
                {
                    var             item      = new ConstantExpressionNode(new OneTimeImpl());
                    IExpressionNode parameter = new LambdaExpressionNode(methodCallExp.Arguments[0], null);
                    return(new MethodCallExpressionNode(item, OneTimeImpl.GetValueMethodName, new[] { parameter }, null));
                }
            }

            var    nodes      = new List <IExpressionNode>();
            var    members    = new List <string>();
            string memberName = node.TryGetMemberName(true, true, nodes, members);

            if (memberName == null)
            {
                var relativeExp = nodes[0] as IRelativeSourceExpressionNode;
                if (relativeExp != null)
                {
                    relativeExp.MergePath(BindingExtensions.MergePath(members));
                    return(relativeExp);
                }

                var methodCall = nodes[0] as IMethodCallExpressionNode;
                if (methodCall != null && methodCall.Target is ResourceExpressionNode)
                {
                    if (RelativeSourceAliases.Contains(methodCall.Method))
                    {
                        if ((methodCall.Arguments.Count == 1 || methodCall.Arguments.Count == 2) &&
                            methodCall.Arguments[0] is IMemberExpressionNode)
                        {
                            int level        = 1;
                            var relativeType = (IMemberExpressionNode)methodCall.Arguments[0];
                            if (methodCall.Arguments.Count == 2)
                            {
                                level = (int)((IConstantExpressionNode)methodCall.Arguments[1]).Value;
                            }
                            return(RelativeSourceExpressionNode.CreateRelativeSource(relativeType.Member, (uint)level,
                                                                                     BindingExtensions.MergePath(members)));
                        }
                    }

                    if (ElementSourceAliases.Contains(methodCall.Method))
                    {
                        if (methodCall.Arguments.Count == 1 && methodCall.Arguments[0] is IMemberExpressionNode)
                        {
                            var elementSource = (IMemberExpressionNode)methodCall.Arguments[0];
                            return(RelativeSourceExpressionNode.CreateElementSource(elementSource.Member,
                                                                                    BindingExtensions.MergePath(members)));
                        }
                    }
                }
            }
            return(node);
        }