I have a Visual Studio solution where I am trying to add the Code Analyzers using build prop file in all projects. I have projects which depends on .NET Core as well as for framework. I have below Directory.Build.Prop file
<Project>
<Choose>
<When Condition="$(UsingMicrosoftNETSdk) == 'true'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory).editorconfig" Link=".editorconfig" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</When>
<Otherwise>
</Otherwise>
</Choose>
</Project>
Now, I am able to distinguish between the .Net core and framework projects using the property UsingMicrosoftNETSdk as all new core projects use SDK style of projects and that is not the case for Framework projects. But I am not exactly sure what should I add in the Otherwise section to add these analyzers to it.
As the used analyzer is platform independent, I am trying to use the package reference or reference element to add it. But it is not working as expected, and I see analyzer are not working, plus it messes up the other references in the Framework project( Get the warning sign on reference and project doesn't build) .
Also I think it has to do with package reference in framework project being managed in packages.config file, but I am not sure if we can apply something to it from build.props
Actually, .NET Framework projects support PackageReference NuGet management format. See this official document.
And if you want to use Directory.Build.props file to install some NuGet packages with packages.config NuGet management format for .NET Framework projects, it is impossible because packages.config NuGet management format uses the packages.config to install packages, and proj file cannot modify packages.config file.
But only PackageReference NuGet management can use proj to install NuGet packages without packages.config file.
Therefore, the only consideration now is how to change the .NETt Framework project that has installed some NuGet packages using packages.config.
Suggestion
Before doing this, you could make a backup for all your projects.
1) So open all your .NET Framework projects on VS, and then right-click on every packages.config file-->click Migrate packages.config into PackageReference.

Then, all your .NET Framework projects could use PackageReference as .NET Core projects did.
2) Then, add these for .NET Framework projects on Directory.Build.Prop file like what you did for .NET Core.
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
Update 1
Before the process, you could make a backup for your solution.
==========================
For ASP.NET framework projects, it can use PackageReference. Just do some modification.
Remember to comment the ProjectTypeGuids property like this:

packages.config and click Migrate packages.config into PackageReference.When you finish this step, unload your project and make ProjectTypeGuids property back.
Try the following steps for every ASP.NET project.
=========================
For C++ projects, the NuGet package Microsoft.CodeAnalysis.NetAnalyzers is not suitable for them, so you have to make a judgement call to remove the C++ project from your current logic operation.
And C++ projects use PlatformToolset property to build projects, while .NET projects do not use it. So this is a good judgement condition.
Use this:
<Project>
<Choose>
<!--exlcude c++ projects and all the net framework and net core projects can use PackageReference-->
<When Condition="'$(PlatformToolset)'==''">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory).editorconfig" Link=".editorconfig" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</When>
<Otherwise>
</Otherwise>
</Choose>
</Project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With