CA2133: Delegates must bind to methods with consistent transparency
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). |
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.
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.
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() { } } }
Note