Пример #1
0
        public static void ReportToUseNameOfIfApplicable(
            SyntaxNodeAnalysisContext context,
            SyntaxNode syntaxNode,
            SourceAttributeInformation attributeInfo,
            ISymbol symbol,
            string stringConstant,
            DiagnosticDescriptor considerNameOfDescriptor)
        {
            var sourceIsAccessible = context.SemanticModel.IsAccessible(
                syntaxNode.GetLocation().SourceSpan.Start,
                symbol);

            if (attributeInfo.IsStringLiteral && sourceIsAccessible)
            {
                var nameOfClassTarget = attributeInfo.SourceType.ToMinimalDisplayString(
                    context.SemanticModel,
                    syntaxNode.GetLocation().SourceSpan.Start);

                var nameOfTarget = attributeInfo.SourceType == context.ContainingSymbol.ContainingType
                    ? stringConstant
                    : $"{nameOfClassTarget}.{stringConstant}";

                var properties = new Dictionary <string, string>
                {
                    { SourceCommonConstants.PropertyKeyNameOfTarget, nameOfTarget }
                };

                context.ReportDiagnostic(Diagnostic.Create(
                                             considerNameOfDescriptor,
                                             syntaxNode.GetLocation(),
                                             properties.ToImmutableDictionary(),
                                             nameOfTarget,
                                             stringConstant));
            }
        }
Пример #2
0
        public static ISymbol?GetMember(SyntaxNodeAnalysisContext context, SourceAttributeInformation attributeInformation)
        {
            if (attributeInformation.SyntaxNode == null || !SyntaxFacts.IsValidIdentifier(attributeInformation.SourceName))
            {
                return(null);
            }

            foreach (var syntaxReference in attributeInformation.SourceType.DeclaringSyntaxReferences)
            {
                if (syntaxReference.GetSyntax() is ClassDeclarationSyntax syntax)
                {
                    var classIdentifier = syntax.Identifier;

                    var symbols = context.SemanticModel.LookupSymbols(
                        classIdentifier.Span.Start,
                        container: attributeInformation.SourceType,
                        name: attributeInformation.SourceName);

                    foreach (var symbol in symbols)
                    {
                        switch (symbol.Kind)
                        {
                        case SymbolKind.Field:
                        case SymbolKind.Property:
                        case SymbolKind.Method:
                            return(symbol);
                        }
                    }
                }
            }

            return(null);
        }
Пример #3
0
        public static ISymbol?GetMember(SourceAttributeInformation attributeInformation)
        {
            if (attributeInformation.SourceName is null || !SyntaxFacts.IsValidIdentifier(attributeInformation.SourceName))
            {
                return(null);
            }

            return(attributeInformation.SourceType.GetMember(attributeInformation.SourceName));
        }