0

I'm trying to create a DiagnosticAnalyzer that detects when a namespace declaration is different from the directory location in a project in VS.

Example:

Solution
  - Project
     - ProjectFolder
        - MyClass.cs

namespace Project.ProjectFolder // Good
namespace Project.OtherSubNamespace // Bad, different from the location

The problem I'm facing right now, is to find the source path (relative to the project) from the current SymbolAnalysisContext in the analyzer. In a codefix provider, you would use the Document class, but I'm not sure if it's possible from a SymbolAnalysisContext.

Philippe Paré
  • 4,279
  • 5
  • 36
  • 56

1 Answers1

0

Use the DeclaringSyntaxReferences property of the symbol being analyzed.

Note that there may be zero, or two or more, references.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I see, with that, I can get the source file's path. But is there a way I can get the current solution directory? With this information, I could make the path relative and check if the relative path corresponds to the namespace. I really only need the project path of the source file! – Philippe Paré Feb 21 '16 at 02:06
  • Not easily, because analyzers can run below the solution layer (and outside VS) – SLaks Feb 21 '16 at 02:57
  • You might be able to grab the VisualStudioWorkspace from the VS global ServiceProvider via MEF, but it would be hacky. – SLaks Feb 21 '16 at 03:01