I am trying to program a code analyzer that looks for types that no other type refers to in Visual Studio 2015.
My problem is that I cannot figure out how to find a list of types without references.
I tried through the DOM, as you can see from the code below, but I don't know where to go, and the current code already seems slow.
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using System.Collections.Immutable; using System.Linq; namespace AlphaSolutions.CodeAnalysis { [DiagnosticAnalyzer(LanguageNames.CSharp)] public class ZeroReferencesDiagnosticAnalyzer : DiagnosticAnalyzer { public const string DiagnosticId = "ZeroReferences"; private static DiagnosticDescriptor rule = new DiagnosticDescriptor( DiagnosticId, title: "Type has zero code references", messageFormat: "Type '{0}' is not referenced within the solution", category: "Naming", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true, description: "Type should have references." ); public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(rule); } } public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction(this.AnalyzeSyntaxNode, SyntaxKind.ClassDeclaration); } private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext obj) { var classDeclaration = obj.Node as ClassDeclarationSyntax; if(classDeclaration == null) { return; } var identifierNameSyntaxes = classDeclaration .DescendantNodes() .OfType<IdentifierNameSyntax>() .ToArray() ; if (identifierNameSyntaxes.Length == 0) { return; }
I also tried SymbolFinder.FindReferencesAsync(namedTypeSymbol, solution) , but cannot figure out how to get a link to Solution .
Response to Microsoft Answers even suggest using the FindReferences method from the Roslyn.Services assembly. But it looks like the build is out of date.
I know that CodeLens, I count links, gaining access to this counter may be an even better solution, but I assume that this is not possible.
Before anyone suggests duplicating a post, this post is NOT a duplicate of this , this, or this . My post is specific to analyzers for the Roslyn compiler.
source share