示例#1
0
        private (SyntaxNode member, string memberName, string signature) GetMemberContainer(Cache doc, SyntaxNode node)
        {
            var method = GetContainer <MethodDeclarationSyntax>(node);

            if (method != null)
            {
                return(method, GetSymbol(doc, method)?.Name, GetSignature(doc, method));
            }

            var constructor = GetContainer <ConstructorDeclarationSyntax>(node);

            if (constructor != null)
            {
                return(constructor, ".ctor", GetSignature(doc, constructor));
            }

            var property = GetContainer <PropertyDeclarationSyntax>(node);

            if (property != null)
            {
                var accessor = GetContainer <AccessorDeclarationSyntax>(node);
                if (accessor != null)
                {
                    var propertySymbol = GetSymbol(doc, accessor);
                    return(property, propertySymbol.Name, GetSignature(doc, accessor));
                }
                var arrowExpression = GetContainer <ArrowExpressionClauseSyntax>(node);
                if (arrowExpression != null)
                {
                    var propertySymbol = GetSymbol(doc, property) as IPropertySymbol;
                    return(property, propertySymbol.GetMethod.Name, GetSignature(doc, property));
                }
            }

            var indexer = GetContainer <IndexerDeclarationSyntax>(node);

            if (indexer != null)
            {
                var indexerSymbol = GetSymbol(doc, indexer) as IPropertySymbol;
                return(indexer, indexerSymbol.Name.Replace("this[]", "get_Item"), GetSignature(doc, indexer));
            }

            var operatorContainer = GetContainer <OperatorDeclarationSyntax>(node);

            if (operatorContainer != null)
            {
                var operatorSymbol = GetSymbol(doc, operatorContainer);
                return(operatorContainer, operatorSymbol.Name, SignatureResolver.BuildSignature((IMethodSymbol)operatorSymbol));
            }
            return(null, null, null);
        }
示例#2
0
        private string GetSignature(Cache doc, SyntaxNode node)
        {
            var symbol = GetSymbol(doc, node);

            if (symbol == null)
            {
                return(null);
            }

            if (symbol.Kind == SymbolKind.Method)
            {
                return(SignatureResolver.BuildSignature((IMethodSymbol)symbol));
            }
            if (symbol.Kind == SymbolKind.Property)
            {
                return(SignatureResolver.BuildSignature((IPropertySymbol)symbol));
            }
            return(null);
        }
示例#3
0
        private MethodCall ResolveMethod(Cache doc, SyntaxNode node, ISymbol symbol)
        {
            string methodName   = symbol.Name;
            string typeName     = symbol.ContainingType.GetFullTypename();
            var    methodSymbol = (IMethodSymbol)symbol;
            string signature    = SignatureResolver.BuildSignature(methodSymbol);

            MethodCall result = GetMethodCall(doc, node, methodName, typeName, signature);

            if (result != null)
            {
                return(result);
            }

            if (!symbol.IsOverride || methodSymbol.OverriddenMethod == null)
            {
                return(null);
            }

            return(ResolveMethod(doc, node, methodSymbol.OverriddenMethod));
        }
示例#4
0
        public MethodCall GetMethodCall(Cache doc, TextSpan textSpan)
        {
            if (AnalyzerModel.CallGraph == null)
            {
                return(null);
            }

            var node = GetExpression(doc.SyntaxRoot.FindNode(textSpan));

            if (node is ConstructorDeclarationSyntax || node is MethodDeclarationSyntax || node is PropertyDeclarationSyntax)
            {
                return(null);
            }

            var symbol = GetSymbol(doc, node);

            if (symbol == null)
            {
                return(null);
            }

            if (symbol.Kind == SymbolKind.Local && node.Parent is ElementAccessExpressionSyntax)
            {
                symbol = GetSymbol(doc, node.Parent);
                if (symbol == null)
                {
                    return(null);
                }
            }

            if (symbol.Kind == SymbolKind.Method)
            {
                return(ResolveMethod(doc, node, symbol));
            }
            if (symbol.Kind == SymbolKind.Property)
            {
                string methodName = GetPropertyAccessorMethod(node, symbol);
                string typeName   = symbol.ContainingType.GetFullTypename();

                return(GetMethodCall(doc, node, methodName, typeName));
            }
            if (symbol.Kind == SymbolKind.NamedType)
            {
                var objectCreationExpression = node.FirstAncestorOrSelf <ObjectCreationExpressionSyntax>();
                if (objectCreationExpression == null)
                {
                    return(null);
                }

                var originalDefinition = symbol.OriginalDefinition;

                symbol = doc.SemanticModel.GetSymbolInfo(objectCreationExpression).Symbol;

                if (originalDefinition != symbol.ContainingType.OriginalDefinition)
                {
                    return(null);
                }

                string methodName = ".ctor";
                string typeName   = symbol.ContainingType.GetFullTypename();
                string signature  = SignatureResolver.BuildSignature((IMethodSymbol)symbol);

                return(GetMethodCall(doc, node, methodName, typeName));
            }
            return(null);
        }