TypeBuilder.AddDeclarativeSecurity メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > TypeBuilder.AddDeclarativeSecurity メソッドの意味・解説 

TypeBuilder.AddDeclarativeSecurity メソッド

この型に宣言セキュリティ追加します

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub AddDeclarativeSecurity ( _
    action As SecurityAction, _
    pset As PermissionSet _
)
Dim instance As TypeBuilder
Dim action As SecurityAction
Dim pset As PermissionSet

instance.AddDeclarativeSecurity(action, pset)
public void AddDeclarativeSecurity (
    SecurityAction action,
    PermissionSet pset
)
public:
void AddDeclarativeSecurity (
    SecurityAction action, 
    PermissionSet^ pset
)
public void AddDeclarativeSecurity (
    SecurityAction action, 
    PermissionSet pset
)
public function AddDeclarativeSecurity (
    action : SecurityAction, 
    pset : PermissionSet
)

パラメータ

action

DemandAssert など、実行するセキュリティ アクション

pset

アクション適用する一連のアクセス許可

例外例外
例外種類条件

ArgumentOutOfRangeException

action無効です。RequestMinimumRequestOptional、および RequestRefuse無効です。

InvalidOperationException

外側の型が CreateType を使用して作成されています。

または

アクセス許可セット pset に、既に AddDeclarativeSecurity追加されたアクション含まれています。

ArgumentNullException

psetnull 参照 (Visual Basic では Nothing) です。

解説解説

AddDeclarativeSecurity は、呼び出すたびにセキュリティ アクション (DemandAssertDeny など) とそのアクション適用するアクセス許可セット指定して複数呼び出すことができます

使用例使用例

次のコード例は、AddDeclarativeSecurity使用してセキュリティ アクション "Demand" を動的な型に追加する方法示してます。

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

Namespace CustomAttribute_Sample

   Class MyApplication
      
      ' Create a dynamic type, MyDynamicClass, that demands
      ' ControlEvidence permission.
      '
      Private Shared Function
 CreateCallee(appDomain As AppDomain) As Type
         ' Create a simple name for the assembly.        
         Dim myAssemblyName As New
 AssemblyName()
         myAssemblyName.Name = "EmittedAssembly"
         ' Create the called dynamic assembly.
         Dim myAssemblyBuilder As AssemblyBuilder
 = _
                  appDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
         Dim myModuleBuilder As ModuleBuilder
 = _
                  myAssemblyBuilder.DefineDynamicModule("EmittedModule")
         ' 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)


         ' Define a private String field named "m_greeting"
 in the type.
         Dim myFieldBuilder As FieldBuilder
 = _
                  myTypeBuilder.DefineField("m_greeting",
 GetType(String), FieldAttributes.Private)
         ' Define constructor.
         Dim constructorArgs() As Type = New
 Type() {GetType(String)}
         Dim myConstructorBuilder As ConstructorBuilder
 = _
                  myTypeBuilder.DefineConstructor(MethodAttributes.Public, _
                  CallingConventions.Standard, constructorArgs)
         ' Generate IL for the method.The constructor stores its argument
 in the private field.
         Dim constructorIL As ILGenerator =
 myConstructorBuilder.GetILGenerator()
         constructorIL.Emit(OpCodes.Ldarg_0)
         constructorIL.Emit(OpCodes.Ldarg_1)
         constructorIL.Emit(OpCodes.Stfld, myFieldBuilder)
         constructorIL.Emit(OpCodes.Ret)
         ' Define property 'Greeting'.
         Dim propertyArgs() As Type
         Dim myPropertyBuilder As PropertyBuilder
 = _
                  myTypeBuilder.DefineProperty("Greeting",
 PropertyAttributes.None, _
                  GetType(String), propertyArgs)
         ' Create the get_Greeting method.
         Dim getGreetingMethod As MethodBuilder
 = _
                  myTypeBuilder.DefineMethod("get_Greeting",
 MethodAttributes.Public Or _
                  MethodAttributes.HideBySig Or MethodAttributes.SpecialName,
 _
                  GetType(String), Nothing)
         ' Generate IL for get_Greeting method.
         Dim methodIL As ILGenerator = getGreetingMethod.GetILGenerator()
         methodIL.Emit(OpCodes.Ldarg_0)
         methodIL.Emit(OpCodes.Ldfld, myFieldBuilder)
         methodIL.Emit(OpCodes.Ret)
         myPropertyBuilder.SetGetMethod(getGreetingMethod)
         Dim myCustomClassType As Type = myTypeBuilder.CreateType()
         Return myCustomClassType
      End Function 'CreateCallee

      <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")>
 _
      Public Shared Sub
 Main()
         Dim customClassType As Type = CreateCallee(Thread.GetDomain())

         Console.WriteLine("Trying to Create Instance with Default
 permission.")
         ' Create an instance of the "MyDynamicClass" class.
         Dim myCustomObject As Object
 = _
                  Activator.CreateInstance(customClassType, New
 Object() {"HelloWorld!!!"})
         ' Get the 'Greeting' property value.
         Dim objectGreeting As Object
 = _
                  customClassType.InvokeMember("Greeting",
 _
                  BindingFlags.GetProperty, Nothing, myCustomObject,
 Nothing)
         Console.WriteLine("Property Value: " &
 objectGreeting.ToString())
         Console.WriteLine("")


         ' Create a permission set that's the same as the set
         ' demanded by the MyDynamicClass class.
         '
         Dim myPermissionSet As New
 PermissionSet(PermissionState.None)
         Dim ce As New SecurityPermission(SecurityPermissionFlag.ControlEvidence)
         myPermissionSet.AddPermission(ce)

         ' Deny the permission set, and then try to create another
         ' instance of the MyDynamicClass class.
         '
         myPermissionSet.Deny()

         Console.WriteLine("Trying to Create Instance after permission
 has been denied.")
         Try
            ' Create an instance of the "MyDynamicClass" class.
            Dim myNewCustomObject As Object
 = _
                     Activator.CreateInstance(customClassType, New
 Object() {"HelloWorld!!!"})
         Catch e As Exception
            Console.WriteLine("Exception: Failed to create Instance")
            Console.WriteLine("'" & e.Message & "'")
         End Try
      End Sub 'Main
   End Class 'MyApplication
End Namespace 'CustomAttribute_Sample
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using System.Security.Permissions;

namespace CustomAttribute_Sample
{

   class MyApplication
   {
      // Create a dynamic type, MyDynamicClass, that demands
      // ControlEvidence permission.
      //
      private static Type CreateCallee(AppDomain
 appDomain)
      {
         // Create a simple name for the assembly.
         AssemblyName myAssemblyName = new AssemblyName();
         myAssemblyName.Name = "EmittedAssembly";
         // Create the called dynamic assembly.
         AssemblyBuilder myAssemblyBuilder =
            appDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
         ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("EmittedModule");
         // 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);


         // Define a private String field named "m_greeting"
 in the type.
         FieldBuilder myFieldBuilder = myTypeBuilder.DefineField("m_greeting"
,
            typeof(String), FieldAttributes.Private);
         // Define constructor.
         Type[] constructorArgs = { typeof(String) };
         ConstructorBuilder myConstructorBuilder = myTypeBuilder.DefineConstructor(
            MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
         // Generate IL for the method.The constructor stores its argument
 in the private field.
         ILGenerator constructorIL = myConstructorBuilder.GetILGenerator();
         constructorIL.Emit(OpCodes.Ldarg_0);
         constructorIL.Emit(OpCodes.Ldarg_1);
         constructorIL.Emit(OpCodes.Stfld, myFieldBuilder);
         constructorIL.Emit(OpCodes.Ret);
         // Define property 'Greeting'.
         Type[] propertyArgs = {};
         PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty("Greeting"
,
            PropertyAttributes.None, typeof(string),propertyArgs);
         // Create the get_Greeting method.
         MethodBuilder getGreetingMethod = myTypeBuilder.DefineMethod("get_Greeting"
,
            MethodAttributes.Public|MethodAttributes.HideBySig|MethodAttributes.SpecialName
,
            typeof(String), null);
         // Generate IL for get_Greeting method.
         ILGenerator methodIL = getGreetingMethod.GetILGenerator();
         methodIL.Emit(OpCodes.Ldarg_0);
         methodIL.Emit(OpCodes.Ldfld, myFieldBuilder);
         methodIL.Emit(OpCodes.Ret);
         myPropertyBuilder.SetGetMethod(getGreetingMethod);
         Type myCustomClassType = myTypeBuilder.CreateType();
         return(myCustomClassType);
      }

      [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
      public static void
 Main()
      {
         Type customClassType = CreateCallee(Thread.GetDomain());
         Console.WriteLine("Trying to Create Instance with Default permission.");
         // Create an instance of the "MyDynamicClass" class.
         Object myCustomObject = Activator.CreateInstance(customClassType,
                                       new object[] {"HelloWorld!!!"});
         // Get 'Greeting' property.
         object objectGreeting =  customClassType.InvokeMember("Greeting"
,
            BindingFlags.GetProperty,
            null,
            myCustomObject,
            null);

         Console.WriteLine("Property Value: " + objectGreeting.ToString());
         Console.WriteLine("");


         // Create a permission set that's the same as the set
         // demanded by the MyDynamicClass class.
         //
         PermissionSet myPermissionSet = new PermissionSet(PermissionState.None);
         myPermissionSet.AddPermission(
             new SecurityPermission(SecurityPermissionFlag.ControlEvidence));

         // Deny the permission set, and then try to create another
         // instance of the MyDynamicClass class.
         //  
         myPermissionSet.Deny();

         Console.WriteLine("Trying to Create Instance after permission has been
 denied.");
         try
         {
             // Create an instance of the "MyDynamicClass"
 class.
             Object myNewCustomObject = Activator.CreateInstance(customClassType
,
                                       new object[] {"HelloWorld!!!"});
         }
         catch( Exception e)
         {
            Console.WriteLine("Exception: Failed to create Instance");
            Console.WriteLine("'" + e.Message + "'");
         }
      }
   }
}
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Security;
using namespace System::Security::Permissions;

// Assumption : Dynamically created class might require reading of ;
// environment variable "TEMP", hence demands permission from
 caller.
Type^ CreateCallee( AppDomain^ appDomain )
{
   
   // Create a simple name for the assembly.
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "EmittedAssembly";
   
   // Create the called dynamic assembly.
   AssemblyBuilder^ myAssemblyBuilder = appDomain->DefineDynamicAssembly( myAssemblyName,
 AssemblyBuilderAccess::Run );
   ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "EmittedModule"
 );
   
   // 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);

   
   // Define a private String field named "m_greeting" in
 the type.
   FieldBuilder^ myFieldBuilder = myTypeBuilder->DefineField( "m_greeting",
 String::typeid, FieldAttributes::Private );
   
   // Define constructor.
   array<Type^>^constructorArgs = {String::typeid};
   ConstructorBuilder^ myConstructorBuilder = myTypeBuilder->DefineConstructor(
 MethodAttributes::Public, CallingConventions::Standard, constructorArgs );
   
   // Generate IL for the method.The constructor stores its argument
 in the private field.
   ILGenerator^ constructorIL = myConstructorBuilder->GetILGenerator();
   constructorIL->Emit( OpCodes::Ldarg_0 );
   constructorIL->Emit( OpCodes::Ldarg_1 );
   constructorIL->Emit( OpCodes::Stfld, myFieldBuilder );
   constructorIL->Emit( OpCodes::Ret );
   
   // Define property 'Greeting'.
   array<Type^>^propertyArgs = gcnew array<Type^>(0);
   PropertyBuilder^ myPropertyBuilder = myTypeBuilder->DefineProperty( "Greeting",
 PropertyAttributes::None, String::typeid, propertyArgs );
   
   // Create the get_Greeting method.
   MethodBuilder^ getGreetingMethod = myTypeBuilder->DefineMethod( "get_Greeting",
 static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::HideBySig
 | MethodAttributes::SpecialName), String::typeid, nullptr );
   
   // Generate IL for get_Greeting method.
   ILGenerator^ methodIL = getGreetingMethod->GetILGenerator();
   methodIL->Emit( OpCodes::Ldarg_0 );
   methodIL->Emit( OpCodes::Ldfld, myFieldBuilder );
   methodIL->Emit( OpCodes::Ret );
   myPropertyBuilder->SetGetMethod( getGreetingMethod );
   Type^ myCustomClassType = myTypeBuilder->CreateType();
   return (myCustomClassType);
}

int main()
{
   Type^ customClassType = CreateCallee( Thread::GetDomain() );
   Console::WriteLine( "Trying to Create Instance with Default permission."
 );
   
   // Create an instance of the "MyDynamicClass" class.
   array<Object^>^temp0 = {"HelloWorld!!!"};
   Object^ myCustomObject = Activator::CreateInstance( customClassType, temp0 );
   
   // Get 'Greeting' property.
   Object^ objectGreeting = customClassType->InvokeMember( "Greeting",
 BindingFlags::GetProperty, nullptr, myCustomObject, nullptr );
   Console::WriteLine( "Property Value : {0}", objectGreeting );
   Console::WriteLine( "" );
   

   // Create a permission set that's the same as the set
   // demanded by the MyDynamicClass class.
   //
   PermissionSet^ myPermissionSet = gcnew PermissionSet( PermissionState::None );
   myPermissionSet->AddPermission(
      gcnew SecurityPermission(SecurityPermissionFlag::ControlEvidence));
   
   // Deny the permission set, and then try to create another
   // instance of the MyDynamicClass class.
   //  
   myPermissionSet->Deny();


   Console::WriteLine( "Trying to Create Instance after permission has been
 denied." );
   try
   {
      // Create an instance of the "MyDynamicClass" class.
      array<Object^>^temp1 = {"HelloWorld!!!"};
      Object^ myNewCustomObject = Activator::CreateInstance( customClassType, temp1
 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: Failed to create Instance" );
      Console::WriteLine( "'{0}'", e->Message );
   }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TypeBuilder クラス
TypeBuilder メンバ
System.Reflection.Emit 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

TypeBuilder.AddDeclarativeSecurity メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TypeBuilder.AddDeclarativeSecurity メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS