Поделиться через


CA2131: Security critical types may not participate in type equivalence

TypeName

CriticalTypesMustNotParticipateInTypeEquivalence

CheckId

CA2131

Category

Microsoft.Security

Breaking Change

Breaking

Cause

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.

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

See Also

Concepts

Security-Transparent Code, Level 2