示例#1
0
        public static IExpression GetMvcLiteral <TExpression>([NotNull] IExpression literal,
                                                              [CanBeNull] out TExpression expression,
                                                              [CanBeNull] out string anonymousPropertyName)
            where TExpression : class, IInvocationInfo
        {
            literal.AssertIsValid("element is not valid");

            expression            = default(TExpression);
            anonymousPropertyName = null;

            var argument = literal.GetContainingNode <IArgument>();

            if (argument == null)
            {
                return(default(IExpression));
            }

            ITreeNode argumentExpression = literal;

            // check anonymous objects
            var analyzer = LanguageManager.Instance.TryGetService <IAnonymousObjectsAnalyser>(literal.Language);

            if (analyzer != null)
            {
                var anonymousExpression =
                    literal.GetContainingNode <ITreeNode>(element => analyzer.IsCreationExpression(element));
                if (anonymousExpression != null)
                {
                    Pair <string, IManagedExpression> memberInitializer =
                        analyzer.GetMemberInitializers(anonymousExpression)
                        .FirstOrDefault(pair => ReferenceEquals(pair.Second, literal));
                    if (ReferenceEquals(memberInitializer.Second, literal))
                    {
                        anonymousPropertyName = memberInitializer.First;
                        argumentExpression    = anonymousExpression;
                    }
                }
            }

            if (argument.Expression != argumentExpression)
            {
                return(default(IExpression));
            }

            if (argument.Invocation is TExpression)
            {
                expression = (TExpression)argument.Invocation;
            }

            // return literal only if expression is suitable (not null and not internal generated)
            return(expression == null ? default(IExpression) : literal);
        }