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

定义

将声明性安全添加到此类型。Adds declarative security to this type.

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)

参数

action
SecurityAction SecurityAction SecurityAction SecurityAction

要执行的安全操作,如 Demand、Assert 等。The security action to be taken such as Demand, Assert, and so on.

pset
PermissionSet PermissionSet PermissionSet PermissionSet

应用此操作的权限集。The set of permissions the action applies to.

异常

action 无效(RequestMinimumRequestOptionalRequestRefuse 无效)。The action is invalid (RequestMinimum, RequestOptional, and RequestRefuse are invalid).

已使用 CreateType() 创建包含类型。The containing type has been created using CreateType().

- 或 --or- 权限集 pset 包含之前由 AddDeclarativeSecurity 添加的操作。The permission set pset contains an action that was added earlier by AddDeclarativeSecurity.

示例

下面的示例演示了如何AddDeclarativeSecurity使用方法,在名为 EmittedExample 的程序集中,使用将SecurityPermissionFlag.ControlEvidence标志添加到名MyDynamicClass为的动态类型的安全SecurityPermission要求。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. 该示例不生成控制台输出;运行后,可以使用Ildasm (IL 拆装器)来检查 EmittedExample。The example produces no console output; after you run it, you can use Ildasm.exe (IL Disassembler) to examine EmittedExample.dll. MyDynamicClass中, .class public auto ansi打开语句以查看声明性权限。In MyDynamicClass, open the .class public auto ansi statement to see the declarative permission.

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 

注解

AddDeclarativeSecurity每次调用时都可以调用多次,指定安全操作(如 Demand、Assert 或 Deny)和一组适用于该操作的权限。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.

备注

在 .NET Framework 版本1.0、1.1 和2.0 中,使用此方法应用于类型的声明性安全特性以旧的 XML 元数据格式存储。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.

适用于