'Add packages using Directory.Build.Prop for .NET Framework

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source