4

I have a DiagnosticAnalyzer and a CodeFixProvider in a class library, In other words a Roslyn Analyzer. I can Nuget package or Vsix package it but I never plan to publish it and I only need to use it in an single mono solution. I was hoping that simply adding the class library as a reference would treat the class library as an "Analyzer" like it would being Nuget installed but it doesn't. Is it possible to use the DiagnosticAnalyzer from the same solution?

Wilhelmina Lohan
  • 2,803
  • 2
  • 29
  • 58
  • I want to flag this a duplicate of https://stackoverflow.com/questions/40780157/how-to-add-a-custom-code-analyzer-to-a-project-without-nuget-or-vsix but the answer here is way better – Wilhelmina Lohan Mar 12 '19 at 20:46

1 Answers1

2

You can add an analyzer by referencing a dll, which should work with one you've built yourself.

  • Navigate to project
  • Expand References
  • Right-click Analyzers
  • Select Add Analyzer...
  • Select Browse... and navigate to the dll

Related Question

Screenshot of Add Analyzer


Edit: For .NET Core and .NET Standard projects that don't have tooling as stated here manually add this snippet to your .csproj

<ItemGroup>
  <Analyzer Include="path\to\Analyzer.dll" />
</ItemGroup>
Toodoo
  • 8,570
  • 6
  • 35
  • 58
scorkla
  • 300
  • 1
  • 9
  • 1
    I don't have "Add Analyzer..." or "Open Active Rule Set" options – Wilhelmina Lohan Mar 12 '19 at 15:58
  • What version of Visual Studio are you using? – scorkla Mar 12 '19 at 16:00
  • 2017 Version 15.9.8 – Wilhelmina Lohan Mar 12 '19 at 16:01
  • Ah, I missed the .net-standard tag. I think that's why you don't have them. Hmm, if I find a way to do it I'll edit my answer. – scorkla Mar 12 '19 at 16:11
  • The analyzer is .net-standard because that is how the official analyzer template makes it a .net-standard class lib. but the project is want to add an analyzer to is a dotnet-core, asp.net core to be exact. asp.net core dosn't seem to have those options even in its own solution. Downloading the Windows Universal features now to see if I get the options on one of thoes. – Wilhelmina Lohan Mar 12 '19 at 16:17
  • 1
    Sorry, I'm a bit confused why that "Add Analyzer..." menu item (so easy! Exactly what I want!) isn't showing up for me? You say it doesn't show for .NET Core or .NET Standard, but... OK when does it show up? I'm using latest VS2019 for both Core and Standard projects and never see it available. – BittermanAndy Nov 11 '19 at 20:33