This documentation is archived and is not being maintained.

MethodBuilder.AddDeclarativeSecurity Method

Adds declarative security to this method.

[Visual Basic]
Public Sub AddDeclarativeSecurity( _
   ByVal action As SecurityAction, _
   ByVal pset As PermissionSet _
)
[C#]
public void AddDeclarativeSecurity(
 SecurityAction action,
 PermissionSet pset
);
[C++]
public: void AddDeclarativeSecurity(
 SecurityAction action,
 PermissionSet* pset
);
[JScript]
public function AddDeclarativeSecurity(
   action : SecurityAction,
 pset : PermissionSet
);

Parameters

action
The security action to be taken (Demand, Assert, and so on).
pset
The set of permissions the action applies to.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException The action is invalid (RequestMinimum, RequestOptional, and RequestRefuse are invalid).
InvalidOperationException The containing type has been created using CreateType or if the permission set pset contains an action that was added earlier by AddDeclarativeSecurity.
ArgumentNullException pset is a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] The code sample below illustrates the contextual use of AddDeclarativeSecurity to require the caller of a method to have unrestricted permissions.

[Visual Basic] 
' 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)


[C#] 
// 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);


[C++] 
// 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(
   S"MyType",
   TypeAttributes::Public);

Type* temp0 [] = {__typeof(int), __typeof(int)};
MethodBuilder*  myMethod1 = myTypeBuilder->DefineMethod(
   S"MyMethod",
   MethodAttributes::Public,
   __typeof(int),
   temp0);

PermissionSet* myMethodPermissions = new PermissionSet(PermissionState::Unrestricted);

myMethod1->AddDeclarativeSecurity(
   SecurityAction::Demand,
   myMethodPermissions);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

MethodBuilder Class | MethodBuilder Members | System.Reflection.Emit Namespace

Show: