MethodBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) MethodBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) MethodBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) MethodBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) Method

Definition

Adds declarative security to this method.

public:
 void AddDeclarativeSecurity(System::Security::Permissions::SecurityAction action, System::Security::PermissionSet ^ pset);
public void AddDeclarativeSecurity (System.Security.Permissions.SecurityAction action, System.Security.PermissionSet pset);
member this.AddDeclarativeSecurity : System.Security.Permissions.SecurityAction * System.Security.PermissionSet -> unit
Public Sub AddDeclarativeSecurity (action As SecurityAction, pset As PermissionSet)

Parameters

action
SecurityAction SecurityAction SecurityAction SecurityAction

The security action to be taken (Demand, Assert, and so on).

pset
PermissionSet PermissionSet PermissionSet PermissionSet

The set of permissions the action applies to.

Exceptions

The action is invalid (RequestMinimum, RequestOptional, and RequestRefuse are invalid).

The containing type has been created using CreateType().

-or-

The permission set pset contains an action that was added earlier by AddDeclarativeSecurity(SecurityAction, PermissionSet).

-or-

For the current method, the IsGenericMethod property is true, but the IsGenericMethodDefinition property is false.

Examples

The code sample below illustrates the contextual use of AddDeclarativeSecurity to require the caller of a method to have unrestricted permissions.

// myModBuilder is an instance of ModuleBuilder.
// Note that for the use of PermissionSet and SecurityAction,
// the namespaces System::Security and System::Security::Permissions
// should be included.

TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( "MyType",
                             TypeAttributes::Public );

array<Type^>^ temp0 = {int::typeid, int::typeid};
MethodBuilder^ myMethod1 = myTypeBuilder->DefineMethod( "MyMethod",
                           MethodAttributes::Public,
                           int::typeid, temp0 );

PermissionSet^ myMethodPermissions = gcnew PermissionSet(
                                     PermissionState::Unrestricted );

myMethod1->AddDeclarativeSecurity( SecurityAction::Demand,
                                   myMethodPermissions );
// myModBuilder is an instance of ModuleBuilder.
// Note that for the use of PermissionSet and SecurityAction,
// the namespaces System.Security and System.Security.Permissions
// should be included.

TypeBuilder myTypeBuilder = myModBuilder.DefineType("MyType",
			    	TypeAttributes.Public);

					
MethodBuilder myMethod1 = myTypeBuilder.DefineMethod("MyMethod",
					MethodAttributes.Public, 
					typeof(int),
					new Type[] 
					{typeof(int), typeof(int)});	

PermissionSet myMethodPermissions = new PermissionSet(
			PermissionState.Unrestricted);

myMethod1.AddDeclarativeSecurity(SecurityAction.Demand,
				 myMethodPermissions);

' myModBuilder is an instance of ModuleBuilder.
' Note that for the use of PermissionSet and SecurityAction,
' the namespaces System.Security and System.Security.Permissions
' should be included.
Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("MyType", _
      TypeAttributes.Public)

Dim myMethod1 As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _
     MethodAttributes.Public, _
     GetType(Integer), _
     New Type() {GetType(Integer), GetType(Integer)})

Dim myMethodPermissions As New PermissionSet(PermissionState.Unrestricted)

myMethod1.AddDeclarativeSecurity(SecurityAction.Demand, myMethodPermissions)

Remarks

AddDeclarativeSecurity can be called several times, with each call specifying a security action (such as Demand, Assert, and Deny) and a set of permissions that the action applies to.

Note

In the .NET Framework versions 1.0, 1.1, and 2.0, the declarative security attributes applied to a method by using the AddDeclarativeSecurity method are stored in the old XML metadata format. See Emitting Declarative Security Attributes.

Applies to