How to add a custom code analyzer to a project without nuget or VSIX?
Asked Answered
S

2

17

I want to write a custom code analyzer in Visual Studio 2015 for a C# ConsoleApplication. For this reason I don't want to create a seperate "Analyzer with Code Fix" project from template, because this requires to add this analyzer in my projects as nuget package.

enter image description here
Is it possible, to add a analyzer reference manually? I would like to reference the analyzer without nuget.

Stamina answered 24/11, 2016 at 7:20 Comment(2)
You can right click on 'Analyzers' and select an analyzer dll. Better question would be why you don't want to use nuget though.Expecting
@Jeroen Vannevel because why bother with nuget if it's only a custom local analyzer?Clinician
L
15

If you add an analyzer as Nuget and check the content of your project, you'll see that only an <Analyzer Include="..." /> item is added. You can do the same manually. Also, you can do this in the .csproj.user file as well, so you can also do it locally, and not commit this change to your SCM.

Loren answered 26/11, 2016 at 20:48 Comment(3)
I've found in VS2017 at least that if you just add an <Analyzer/> tag to your project, you only get warnings when you build, not as you type. You seem to need to have the nuget package in the packages.config and the <Analyzer/> tag to get the warnings both when you're editing code and compiling.Burnette
I only get "<PackageReference Include="..." What are you doing to get this? Its not working for me.Ilke
Beware that <Analyzer Include="..." /> requires a path to an assembly, and won't work with csprojUnderscore
B
13

You can add analyzer via project reference with OutputItemType set to Analyzer:

<ItemGroup>
  <ProjectReference Include="..\Analyzers\A1.csproj">
     <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
     <OutputItemType>Analyzer</OutputItemType>
   </ProjectReference>
</ItemGroup>

Or via element attributes:

<ItemGroup>
    <ProjectReference Include="..\Analyzers\A1.csproj" OutputItemType="Analyzer"
                      ReferenceOutputAssembly="false"/>
</ItemGroup>
Boudoir answered 2/1, 2023 at 18:33 Comment(1)
works in VS2022Helvetii

© 2022 - 2025 — McMap. All rights reserved.