Events
17 Mar, 21 - 21 Mar, 10
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Property | Value |
---|---|
Rule ID | CA1812 |
Title | Avoid uninstantiated internal classes |
Category | Performance |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | No |
An internal (assembly-level) type is never instantiated.
This rule tries to locate a call to one of the constructors of the type and reports a violation if no call is found.
The following types are not examined by this rule:
static
methods.If you apply System.Runtime.CompilerServices.InternalsVisibleToAttribute to the assembly that's being analyzed, this rule doesn't flag types that are marked as internal
(Friend
in Visual Basic) by default, because a field may be used by a friend assembly. To analyze the assembly anyway, see Configure code to analyze.
To fix a violation of this rule, remove the type or add code that uses it. If the type contains only static
methods, add the static
modifier to the type to prevent the compiler from emitting a default public instance constructor.
It is safe to suppress a warning from this rule. We recommend that you suppress this warning in the following situations:
The class is created through late-bound reflection methods such as System.Activator.CreateInstance.
The class is registered in an inversion of control (IoC) container as part of the dependency injection pattern.
The class is created automatically by the runtime or ASP.NET. Some examples of automatically created classes are those that implement System.Configuration.IConfigurationSectionHandler or System.Web.IHttpHandler.
The class is used as a type parameter in a class definition and has a new
constraint. The following example will be flagged by rule CA1812:
internal class MyClass
{
public void DoSomething()
{
}
}
public class MyGeneric<T> where T : new()
{
public T Create()
{
return new T();
}
}
MyGeneric<MyClass> mc = new MyGeneric<MyClass>();
mc.Create();
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1812
// The code that's violating the rule is on this line.
#pragma warning restore CA1812
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1812.severity = none
For more information, see How to suppress code analysis warnings.
Use the following option to configure which parts of your codebase to run this rule on.
You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Performance) that it applies to. For more information, see Code quality rule configuration options.
By default, this rule is disabled if the assembly being analyzed uses InternalsVisibleToAttribute to expose its internal symbols. You can set the ignore_internalsvisibleto option to change the configuration. To specify that the rule should run even if the assembly is marked with InternalsVisibleToAttribute, add the following key-value pair to an .editorconfig file in your project:
dotnet_code_quality.CAXXXX.ignore_internalsvisibleto = true
Note
Replace the XXXX
part of CAXXXX
with the ID of the applicable rule.
This option is available starting in .NET 8.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 21 - 21 Mar, 10
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now