CA2131: Security critical types may not participate in type equivalence

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see CA2131: Security critical types may not participate in type equivalence on docs.microsoft.com.

TypeNameCriticalTypesMustNotParticipateInTypeEquivalence
CheckIdCA2131
CategoryMicrosoft.Security
Breaking ChangeBreaking

A type participates in type equivalence and a either the type itself, or a member or field of the type, is marked with the SecurityCriticalAttribute attribute.

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.

To fix a violation of this rule, remove the SecurityCritical attribute.

Do not suppress a warning from this rule.

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;
    }
}

Security-Transparent Code, Level 2

Show: