This documentation is archived and is not being maintained.

TypeBuilder.AddDeclarativeSecurity Method

Adds declarative security to this type.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public void AddDeclarativeSecurity(
	SecurityAction action,
	PermissionSet pset


Type: System.Security.Permissions.SecurityAction
The security action to be taken such as Demand, Assert, and so on.
Type: System.Security.PermissionSet
The set of permissions the action applies to.


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


The containing type has been created using CreateType.


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


pset is null.

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.


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 (MSIL Disassembler) to examine EmittedExample.dll. In MyDynamicClass, open the .class public auto ansi statement to see the declarative permission.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using System.Security.Permissions;

namespace CustomAttribute_Sample
   public class MyApplication 
      static void Main()
         // Create a simple name for the assembly, and create the assembly and module.
         AssemblyName myAssemblyName = new AssemblyName("EmittedAssembly");
         AssemblyBuilder myAssemblyBuilder =
            AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
         ModuleBuilder myModuleBuilder = 
            myAssemblyBuilder.DefineDynamicModule("EmittedAssembly", "EmittedAssembly.dll");

         // Define a public class named "MyDynamicClass" in the assembly.
         TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyDynamicClass",

         // Create a permission set and add a security permission
         // with the ControlEvidence flag.
         PermissionSet myPermissionSet = new PermissionSet(PermissionState.None);
             new SecurityPermission(SecurityPermissionFlag.ControlEvidence));

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

         Type myType = myTypeBuilder.CreateType();

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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