CA2131: Security critical types might not participate in type equivalence
Item | Value |
---|---|
RuleId | CA2131 |
Category | Microsoft.Security |
Breaking change | Breaking |
Cause
A type participates in type equivalence and either the type itself, or a member or field of the type, is marked with the SecurityCriticalAttribute attribute.
Note
This rule has been deprecated. For more information, see Deprecated rules.
Rule description
This rule fires on any critical types or types that contain critical methods or fields that are participating in type equivalence. When the CLR detects such a type, it fails to load it with a TypeLoadException at run time. Typically, this rule fires only when users implement type equivalence manually rather than by relying on tlbimp and the compilers to do the type equivalence.
How to fix violations
To fix a violation of this rule, remove the SecurityCritical attribute.
When to suppress warnings
Do not suppress a warning from this rule.
Example
The following examples demonstrate an interface, a method, and a field that will cause this rule to fire.
using System;
using System.Security;
using System.Runtime.InteropServices;
[assembly: SecurityRules(SecurityRuleSet.Level2)]
[assembly: AllowPartiallyTrustedCallers]
namespace TransparencyWarningsDemo
{
// CA2131 error - critical type participating in equivilance
[SecurityCritical]
[TypeIdentifier("3a5b6203-2bf1-4f83-b5b4-1bdc334ad3ea", "ICriticalEquivilentInterface")]
public interface ICriticalEquivilentInterface
{
void Method1();
}
[TypeIdentifier("3a5b6203-2bf1-4f83-b5b4-1bdc334ad3ea", "ITransparentEquivilentInterface")]
public interface ITransparentEquivilentInterface
{
// CA2131 error - critical method in a type participating in equivilance
[SecurityCritical]
void CriticalMethod();
}
[SecurityCritical]
[TypeIdentifier("3a5b6203-2bf1-4f83-b5b4-1bdc334ad3ea", "ICriticalEquivilentInterface")]
public struct EquivilentStruct
{
// CA2131 error - critical field in a type participating in equivalence
[SecurityCritical]
public int CriticalField;
}
}