Esempio n. 1
0
        public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data)
        {
            Expression targetObject = invocationExpression.TargetObject;
            string     methodName   = null;

            if (targetObject is IdentifierExpression)
            {
                methodName = ((IdentifierExpression)targetObject).Identifier;
            }
            else if (targetObject is FieldReferenceExpression)
            {
                methodName = ((FieldReferenceExpression)targetObject).FieldName;
            }

            if (methodName.StartsWith("set") || methodName.StartsWith("get"))
            {
                if (targetObject is IdentifierExpression)
                {
                    TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration));
                    string          key;

                    if (ContainsReferences(typeDeclaration, methodName, out key))
                    {
                        IdentifierExpression identifierExpression = new IdentifierExpression((string)CodeBase.References[key]);
                        if (methodName.StartsWith("get"))
                        {
                            identifierExpression.Parent = invocationExpression.Parent;
                            ReplaceCurrentNode(identifierExpression);
                        }
                        else if (methodName.StartsWith("set"))
                        {
                            Expression           setValue   = (Expression)invocationExpression.Arguments[0];
                            AssignmentExpression assignment = new AssignmentExpression(identifierExpression, AssignmentOperatorType.Assign, setValue);
                            assignment.Parent = invocationExpression.Parent;
                            ReplaceCurrentNode(assignment);
                            assignment.AcceptVisitor(this, data);
                        }
                    }
                }
                else if (targetObject is FieldReferenceExpression)
                {
                    Expression    target      = ((FieldReferenceExpression)targetObject).TargetObject;
                    TypeReference invokerType = GetExpressionType(target);
                    if (invokerType != null)
                    {
                        string fullName = GetFullName(invokerType);

                        string key = fullName + "." + methodName;
                        if (CodeBase.References.Contains(key))
                        {
                            string property = (string)CodeBase.References[key];
                            FieldReferenceExpression replaced = new FieldReferenceExpression(target, property);
                            if (methodName.StartsWith("get"))
                            {
                                replaced.Parent = invocationExpression.Parent;
                                ReplaceCurrentNode(replaced);
                                replaced.AcceptVisitor(this, data);
                            }
                            else
                            {
                                Expression           setValue   = (Expression)invocationExpression.Arguments[0];
                                AssignmentExpression assignment = new AssignmentExpression(replaced, AssignmentOperatorType.Assign, setValue);
                                assignment.Parent = invocationExpression.Parent;
                                ReplaceCurrentNode(assignment);
                                assignment.AcceptVisitor(this, data);
                            }
                        }
                    }
                }
            }
            return(base.TrackedVisitInvocationExpression(invocationExpression, data));
        }