All rules
CA2255Usage Enabled by default: As warning

The ModuleInitializer attribute should not be used in libraries

The ModuleInitializer attribute should not be used in libraries

Microsoft docs

Description

Module initializers are intended to be used by application code to ensure an application's components are initialized before the application code begins executing. If library code declares a method with the System.Runtime.CompilerServices.ModuleInitializerAttribute, it can interfere with application initialization and also lead to limitations in that application's trimming abilities. Library code should therefore not utilize the System.Runtime.CompilerServices.ModuleInitializerAttribute attribute.

Cause

Applying System.Runtime.CompilerServices.ModuleInitializerAttribute to a method within a Class Library.

How to fix violations

Instead of using methods marked with System.Runtime.CompilerServices.ModuleInitializerAttribute, the library should expose methods that can be used to initialize any components within the library and allow the application to invoke the method during application initialization.

Example

#pragma warning disable CA2255
// The code that's violating the rule is on this line.
#pragma warning restore CA2255

When to suppress

It is safe to suppress warnings from this rule if a solution uses a Class Library for code factoring purposes, and the System.Runtime.CompilerServices.ModuleInitializerAttribute method is not part of a shared or distributed library or package.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0