private static void Handle(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.Node is AttributeSyntax attribute &&
         Attribute.IsType(attribute, KnownSymbol.DependencyAttribute, context.SemanticModel, context.CancellationToken) &&
         Attribute.TryFindArgument(attribute, 0, "dependentAssemblyArgument", out var argument) &&
         context.SemanticModel.TryGetConstantValue(argument.Expression, context.CancellationToken, out string assemblyName) &&
         !context.Compilation.ReferencedAssemblyNames.TryFirst(x => x.Name == assemblyName, out _))
     {
         context.ReportDiagnostic(Diagnostic.Create(REFL032DependencyMustExist.Descriptor, argument.GetLocation()));
     }
 }
示例#2
0
        /// <summary>
        /// Attempt to retrieve information about the application of the
        /// DefaultMemberAttribute:
        ///
        ///  - Specified member name
        ///  - Location of attribute
        ///  - Type symbol of type with attribute applied
        ///
        /// Does not throw on failure.
        /// </summary>
        /// <param name="context">Context of attribute application.</param>
        /// <param name="memberName">Member name specified by attribute.</param>
        /// <param name="location">Location of attribute application.</param>
        /// <param name="typeSymbol">Attribute target.</param>
        /// <returns>Success</returns>
        private static bool TryGetAttributeAndTypeInfo(
            SyntaxNodeAnalysisContext context,
            out string memberName,
            out Location location,
            out ITypeSymbol typeSymbol)
        {
            memberName = default(string);
            location   = default(Location);
            typeSymbol = default(ITypeSymbol);

            if (!(context.Node is AttributeSyntax attribute) ||
                !Attribute.IsType(attribute, KnownSymbol.DefaultMemberAttribute, context.SemanticModel, context.CancellationToken))
            {
                return(false);
            }

            if (!Attribute.TryFindArgument(attribute, 0, "memberName", out var argument) ||
                !context.SemanticModel.TryGetConstantValue(argument.Expression, context.CancellationToken, out string workingMemberName))
            {
                return(false);
            }

            if (!attribute.TryFirstAncestor(out ClassDeclarationSyntax classDeclaration))
            {
                return(false);
            }

            if (!context.SemanticModel.TryGetSymbol(classDeclaration, context.CancellationToken, out ITypeSymbol workingTypeSymbol))
            {
                return(false);
            }

            memberName = workingMemberName;
            location   = argument.GetLocation();
            typeSymbol = workingTypeSymbol;
            return(true);
        }