Compartir a través de


TypeBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) Método

Definición

Agrega seguridad declarativa a este tipo.

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)

Parámetros

action
SecurityAction

Acción de seguridad que se va a realizar, como Demand, Assert, etc.

pset
PermissionSet

El conjunto de permisos al que se aplica la acción.

Excepciones

El action no es válido (RequestMinimum, RequestOptional y RequestRefuse no son válidos).

Tipo contenedor que se ha creado mediante CreateType().

o bien

El conjunto de permisos pset contiene una acción que AddDeclarativeSecurity agregó anteriormente.

pset es null.

Ejemplos

En el ejemplo siguiente se muestra el uso del método para agregar una demanda de AddDeclarativeSecurity seguridad para SecurityPermission con la SecurityPermissionFlag.ControlEvidence marca a un tipo dinámico denominado MyDynamicClass, en un ensamblado denominado EmittedExample.dll. En el ejemplo no se genera ninguna salida de consola; después de ejecutarlo, puede usar Ildasm.exe (Desensamblador de IL) para examinar EmittedExample.dll. En MyDynamicClass, abra la .class public auto ansi instrucción para ver el permiso declarativo.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Security;
using namespace System::Security::Permissions;

int main()
{
   // Create a simple name for the assembly; create the assembly and module.
   AssemblyName^ myAssemblyName = gcnew 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", TypeAttributes::Public );
   

   // Create a permission set and add a security permission
   // with the ControlEvidence flag.
   //
   PermissionSet^ myPermissionSet = gcnew PermissionSet(PermissionState::None);
   myPermissionSet->AddPermission(
      gcnew 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();
   myAssemblyBuilder->Save("EmittedAssembly.dll");
}
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",
            TypeAttributes.Public);

         // Create a permission set and add a security permission
         // with the ControlEvidence flag.
         //
         PermissionSet myPermissionSet = new PermissionSet(PermissionState.None);
         myPermissionSet.AddPermission(
             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();
         myAssemblyBuilder.Save("EmittedAssembly.dll");
      }
   }
}
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

Comentarios

AddDeclarativeSecurity se puede llamar varias veces con cada llamada que especifica una acción de seguridad (como Demanda, Aserción o Denegación) y un conjunto de permisos que se aplican a la acción.

Nota

En las versiones 1.0, 1.1 y 2.0 de .NET Framework, los atributos de seguridad declarativos aplicados a un tipo mediante este método se almacenan en el formato de metadatos XML antiguo.

Se aplica a