コード例 #1
0
        /// <summary>
        /// Returns the list of invocations from the given symbol.
        /// </summary>
        /// <param name="symbol">Symbol</param>
        /// <param name="method">Method</param>
        /// <returns>List of invocations</returns>
        private static List <InvocationExpressionSyntax> GetInvocationsFromSymbol(ISymbol symbol,
                                                                                  MethodDeclarationSyntax method)
        {
            var invocations = new List <InvocationExpressionSyntax>();
            var locations   = SymbolFinder.FindReferencesAsync(symbol, ProgramInfo.Solution).
                              Result.First().Locations;

            foreach (var loc in locations)
            {
                SyntaxNode node = null;
                try
                {
                    node = method.FindNode(loc.Location.SourceSpan);
                }
                catch
                {
                    continue;
                }

                var invocation = node.AncestorsAndSelf().OfType <InvocationExpressionSyntax>().FirstOrDefault();
                if (invocation != null && invocation.DescendantNodes().OfType <IdentifierNameSyntax>().
                    First().Identifier.ValueText.Equals(symbol.Name))
                {
                    invocations.Add(invocation);
                }
            }

            return(invocations);
        }
        private MethodDeclarationSyntax UpdateMethodBody(MethodDeclarationSyntax method, IEnumerable <ParameterSyntax> parameters, string parameterObjectName, SemanticModel semanticModel)
        {
            if (method.Body == null)
            {
                return(method);
            }

            var identifiers = method.Body.DescendantTokens()
                              .OfType <SyntaxToken>()
                              .Where(x => x.Kind() == SyntaxKind.IdentifierToken)
                              .Where(x => parameters.Any(y => y.Identifier.ValueText == x.ValueText));

            var parameterIdentiferies = new List <SyntaxToken>(identifiers.Count());

            foreach (var token in identifiers)
            {
                var node   = method.FindNode(token.Span);
                var symbol = semanticModel.GetSymbolInfo(node).Symbol;
                if (symbol is IParameterSymbol)
                {
                    parameterIdentiferies.Add(token);
                }
            }

            method = method.WithBody(method.Body.ReplaceTokens(parameterIdentiferies, (x, _) => AddPrefixToIdentifier(x, parameterObjectName.ToLowerFirst())));
            return(method);
        }