private static IEnumerable <MethodCallExpression> GetChainedMethodCalls(
                MethodCallExpression methodCall)
            {
                while (methodCall != null)
                {
                    yield return(methodCall);

                    methodCall = methodCall.GetSubject() as MethodCallExpression;
                }
            }
Esempio n. 2
0
        private static string GetMethodCallSubject(
            MethodCallExpression methodCall,
            TranslationContext context,
            out IEnumerable <Expression> arguments)
        {
            if (methodCall.Object == null)
            {
                return(GetStaticMethodCallSubject(methodCall, context, out arguments));
            }

            arguments = methodCall.Arguments;

            return(context.Translate(methodCall.Object));
        }
Esempio n. 3
0
        private static string GetStaticMethodCallSubject(
            MethodCallExpression methodCall,
            TranslationContext context,
            out IEnumerable <Expression> arguments)
        {
            if (methodCall.Method.IsExtensionMethod())
            {
                var subject = methodCall.Arguments.First();
                arguments = methodCall.Arguments.Skip(1).ToArray();

                return(context.Translate(subject));
            }

            arguments = methodCall.Arguments;

            // ReSharper disable once PossibleNullReferenceException
            return(methodCall.Method.DeclaringType.GetFriendlyName());
        }
            protected override Expression VisitMethodCall(MethodCallExpression methodCall)
            {
                if (!ChainedMethodCalls.Contains(methodCall))
                {
                    var methodCallChain = GetChainedMethodCalls(methodCall).ToArray();

                    if (methodCallChain.Length > 1)
                    {
                        if (methodCallChain.Length > 2)
                        {
                            ChainedMethodCalls.AddRange(methodCallChain);
                        }
                        else if (methodCallChain[0].ToString().Contains(" ... "))
                        {
                            // Expression.ToString() replaces multiple lines with ' ... ';
                            // potential fragile, but works unless MS change it:
                            ChainedMethodCalls.AddRange(methodCallChain);
                        }
                    }
                }

                return(base.VisitMethodCall(methodCall));
            }
Esempio n. 5
0
 public static string GetMethodCallSubject(MethodCallExpression methodCall, TranslationContext context)
 {
     // ReSharper disable once UnusedVariable
     return(GetMethodCallSubject(methodCall, context, out var arguments));
 }
Esempio n. 6
0
 /// <summary>
 /// Returns the Expression representing the subject of the given <paramref name="methodCall"/>.
 /// </summary>
 /// <param name="methodCall">
 /// The Expression representing the method call the subject of which should be retrieved.
 /// </param>
 /// <returns>
 /// The Expression representing the subject of the given <paramref name="methodCall"/>.
 /// </returns>
 public static Expression GetSubject(this MethodCallExpression methodCall)
 {
     return(methodCall.Method.IsExtensionMethod()
          ? methodCall.Arguments.First() : methodCall.Object);
 }