Home > OS >  "CA 1852 Type X can be sealed because it has no subtypes in its containing assembly and is not
"CA 1852 Type X can be sealed because it has no subtypes in its containing assembly and is not

Time:01-02

I tried to compile a .NET app I got from GitHub and got a "CA 1852 Type X can be sealed because it has no subtypes in its containing assembly and is not externally visible" error. This seems to be a code style error instead of a real error.

To try to suppress it, I added the 1852 line to the .csproj file:

<PropertyGroup>
    <NoWarn>$(NoWarn);1591</NoWarn>
    <NoWarn>$(NoWarn);1852</NoWarn>
  </PropertyGroup>

plus this to the .editorconfig file:

dotnet_diagnostic.CS1852.severity = none

'dotnet_diagnostic' might be incorrect. Both didn't work.

So what worked was adding this above the error line:

#pragma warning disable CA1852

My question is what triggered this type of error to show up? .editorconfig doesn't have anything for 1852, unless it's a computer wide setting on my machine or something?

Also, how do I suppress it for the whole solution or project?

CodePudding user response:

CA1852 is not a compiler error or warning. It is a static code analysis warning where the analysis is done by "Rosyln". If it is actually preventing you from compiling code, then you have WarningsAsErrors turned on.

Regardless, you can follow the advice or suppress the warning. All of @jmcilhinney's comments are correct and relevant, and the fact that the warning is new is likely the trigger.

To my knowledge, there is no built-in global way to suppress a warning solution wide. That said, you can effect the same result by including a "standard" bit of configuration stored in a separate file. It's still not technically global because each .csproj will need to be modified to import the settings.

To do that, create a file such as CommonStuff.xml with

<PropertyGroup>
    <NoWarn>1852</NoWarn>
</PropertyGroup>

in it. Then modify each of your .csproj to include this file:

<Import Project="$(SolutionDir)\CommonStuff.xml" />

In the above, I chose SolutionDir as that is probably the best place to add this file and reference it from all the projects. Other paths can be used; see Common macros... in the Microsoft documentation. The other good choice would be ProjectDir if that's the level of granularity you need.

  • Related