This attribute can be applied to methods that want to call into native code without incurring the cost of a security check in doing so: only code that has been granted SecurityPermission.UnmanagedCode permission may do this.
Object
Attribute
SuppressUnmanagedCodeSecurityAttribute
[Visual Basic] Public Class SuppressUnmanagedCodeSecurityAttribute Inherits Attribute [C#] public class SuppressUnmanagedCodeSecurityAttribute : Attribute [C++] public __gc class SuppressUnmanagedCodeSecurityAttribute : public Attribute [JScript] public class SuppressUnmanagedCodeSecurityAttribute extends Attribute
This attribute can be applied to methods that want to call into native code without incurring the cost of a security check in doing so: only code that has been granted SecurityPermission.UnmanagedCode permission may do this. Putting this method to a class, module, or assembly has the effect of applying to all contained methods.
Normally whenever managed code call into unmanaged- be it by P/Invoke or COM interop into native code- there is a demand for SecurityPermission.UnmanagedCode permission to ensure that all callers have the necessary permission to allow this. By applying this explicit attribute developers can suppress this demand, in effect taking responsibility that the transition into unmanaged code is sufficiently secured by other means.
This attribute is useful in the case of implementing a class that provides access to system resources through unmanaged code. Typically, methods on the class perform some security permission demand (if appropriate) and then only after asserting their permission to call unmanaged code- which their callers ostensibly do not have- they proceed to call into unmanaged code to fulfill the request. When the assertion of unmanaged code permission is only to enable unmanaged code calls within the method itself it is much more efficient to simply skip the demand since the code would always assert its permission to do so in any case. Thus, for such library code the use of this attribute allows the assertion and subsequent demands associated with calling unmanaged code all to be skipped at runtime, resulting in a substancial performance savings.
This is not a declarative security attribute, but regular attribute (that is, it derives from Attribute, not SecurityAttribute).
Namespace: System.Security
Assembly: mscorlib.dll
SuppressUnmanagedCodeSecurityAttribute Members | System.Security Namespace