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のセキュリティ確認要求を追加するメソッドをSecurityPermissionで、SecurityPermissionFlag.ControlEvidenceという名前の動的な型をフラグMyDynamicClass、EmittedExample.dll という名前のアセンブリにします。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.exe (IL Disassembler) EmittedExample.dll を確認します。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
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.

適用対象