Partager via


CA2133: Delegates must bind to methods with consistent transparency

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName DelegatesMustBindWithConsistentTransparency
CheckId CA2133
Category Microsoft.Security
Breaking Change Breaking

Note

This warning is only applied to code that is running the CoreCLR (the version of the CLR that is specific to Silverlight Web applications).

Cause

This warning fires on a method that binds a delegate that is marked with the SecurityCriticalAttribute to a method that is transparent or that is marked with the SecuritySafeCriticalAttribute. The warning also fires a method that binds a delegate that is transparent or safe-critical to a critical method.

Rule Description

Delegate types and the methods that they bind to must have consistent transparency. Transparent and safe-critical delegates may only bind to other transparent or safe-critical methods. Similarly, critical delegates may only bind to critical methods. These binding rules ensure that the only code that can invoke a method via a delegate could have also invoked the same method directly. For example, binding rules prevent transparent code from calling critical code directly via a transparent delegate.

How to Fix Violations

To fix a violation of this warning, change the transparency of the delegate or of the method that it binds so that the transparency of the two are equivalent.

When to Suppress Warnings

Do not suppress a warning from this rule.

Code

using System;
using System.Security;

namespace TransparencyWarningsDemo
{

    public delegate void TransparentDelegate();

    [SecurityCritical]
    public delegate void CriticalDelegate();

    public class TransparentType
    {
        void DelegateBinder()
        {
            // CA2133 violation - binding a transparent delegate to a critical method
            TransparentDelegate td = new TransparentDelegate(CriticalTarget);

            // CA2133 violation - binding a critical delegate to a transparent method
            CriticalDelegate cd = new CriticalDelegate(TransparentTarget);
        }

        [SecurityCritical]
        void CriticalTarget() { }

        void TransparentTarget() { }
    }
}