好的,我在一个项目中运行了这个分析器:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class GipAnalyzerAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "TestAnalyzer";
private static readonly string Title = "Using directive";
private static readonly string MessageFormat = "Please do not use this namespace in that solution.";
private static readonly string Description = "Some namespace should not be used in certain projects";
private const string Category = "Using Directives";
private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.UsingDirective);
}
private void AnalyzeSyntax(SyntaxNodeAnalysisContext obj)
{
var directive = (UsingDirectiveSyntax) obj.Node;
var qualifiedName = directive.Name;
var qualifiedNameString = qualifiedName.ToFullString();
if (qualifiedNameString.StartsWith("Name.Space.1") ||
qualifiedNameString.StartsWith("Name.Space.2"))
{
// produce a diagnostic.
var diagnostic = Diagnostic.Create(Rule, qualifiedName.GetLocation(), qualifiedName);
obj.ReportDiagnostic(diagnostic);
}
}
}
在项目中有多个文件之前,一切都运行良好。例如,对于项目中的两个文件,可以正确识别诊断,但未显示诊断。
任何想法 ?