Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TypeBuilder.AddDeclarativeSecurity Method

Adds declarative security to this type.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Sub AddDeclarativeSecurity ( _
	action As SecurityAction, _
	pset As PermissionSet _
)

Parameters

action
Type: System.Security.Permissions.SecurityAction

The security action to be taken such as Demand, Assert, and so on.

pset
Type: System.Security.PermissionSet

The set of permissions the action applies to.

ExceptionCondition
ArgumentOutOfRangeException

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

InvalidOperationException

The containing type has been created using CreateType.

-or-

The permission set pset contains an action that was added earlier by AddDeclarativeSecurity.

ArgumentNullException

pset is Nothing.

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

NoteNote

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

The following example demonstrates the use of the AddDeclarativeSecurity method to add a security demand for SecurityPermission with the SecurityPermissionFlag.ControlEvidence flag to a dynamic type named MyDynamicClass, in an assembly named EmittedExample.dll. The example produces no console output; after you run it, you can use Ildasm.exe (IL Disassembler) to examine EmittedExample.dll. In MyDynamicClass, open the .class public auto ansi statement to see the declarative permission.

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security
Imports System.Security.Permissions

Namespace CustomAttribute_Sample

   Class MyApplication

      Shared Sub Main()
         ' Create a simple name for the assembly; create the assembly and the module.         
         Dim myAssemblyName As New AssemblyName("EmittedAssembly")
         Dim myAssemblyBuilder As AssemblyBuilder = _
                  AppDomain.CurrentDomain.DefineDynamicAssembly( _
                          myAssemblyName, AssemblyBuilderAccess.RunAndSave)
         Dim myModuleBuilder As ModuleBuilder = _
                  myAssemblyBuilder.DefineDynamicModule("EmittedAssembly", "EmittedAssembly.dll")

         ' Define a public class named "MyDynamicClass" in the assembly. 
         Dim myTypeBuilder As TypeBuilder = _
                  myModuleBuilder.DefineType("MyDynamicClass", TypeAttributes.Public)


         ' Create a permission set and add a security permission 
         ' with the ControlEvidence flag. 
         
         Dim myPermissionSet As New PermissionSet(PermissionState.None)
         Dim ce As New SecurityPermission(SecurityPermissionFlag.ControlEvidence)
         myPermissionSet.AddPermission(ce)

         ' Add the permission set to the MyDynamicClass type, 
         ' as a declarative security demand. 
         '
         myTypeBuilder.AddDeclarativeSecurity(SecurityAction.Demand, myPermissionSet)


         Dim myType As Type = myTypeBuilder.CreateType()
         myAssemblyBuilder.Save("EmittedAssembly.dll")
      End Sub  
   End Class  
End Namespace

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.