示例#1
0
        public override void ExitMethodInvocationExpression(MethodInvocationExpression methodInvocationExpression)
        {
            var targetMethod = methodInvocationExpression.Target;

            if (targetMethod.Modifiers.Contains(MemberModifier.Static))
            {
                return;
            }

            SimpleNameExpression methodTarget   = null;
            IExpression          thisExpression = null;

            switch (methodInvocationExpression.Base)
            {
            case MemberExpression memberExpression:
                methodTarget = new SimpleNameExpression(memberExpression.Context, memberExpression.Name)
                {
                    Declaration = targetMethod,
                    Type        = new MethodReferenceType(targetMethod)
                };
                thisExpression = memberExpression.Base;
                break;

            case SimpleNameExpression simpleNameExpression:
                //Will only happen if called from another class member
                methodTarget = simpleNameExpression;
                var containingMethod = simpleNameExpression.NearestAncestorOfType <MethodDeclaration>();
                var thisParameter    = containingMethod.Variables.First();
                thisExpression = new SimpleNameExpression(simpleNameExpression.Context, thisParameter.Name)
                {
                    Declaration = thisParameter,
                    Type        = thisParameter.Type
                };
                break;
            }

            methodInvocationExpression.Base.ReplaceWith(methodTarget);
            var addBeforeNode = methodInvocationExpression.Arguments.FirstOrDefault();

            if (addBeforeNode != null)
            {
                methodInvocationExpression.AddChildBefore(addBeforeNode, thisExpression);
            }
            else
            {
                methodInvocationExpression.AddChild(thisExpression);
            }
        }