ConstructorBuilder.SetCustomAttribute メソッド

カスタム属性を設定します。

オーバーロードの一覧

カスタム属性ビルダを使用して、カスタム属性を設定します。

[Visual Basic] Overloads Public Sub SetCustomAttribute(CustomAttributeBuilder)

[C#] public void SetCustomAttribute(CustomAttributeBuilder);

[C++] public: void SetCustomAttribute(CustomAttributeBuilder*);

[JScript] public function SetCustomAttribute(CustomAttributeBuilder);

指定されたカスタム属性 BLOB を使用して、カスタム属性を設定します。

[Visual Basic] Overloads Public Sub SetCustomAttribute(ConstructorInfo, Byte())

[C#] public void SetCustomAttribute(ConstructorInfo, byte[]);

[C++] public: void SetCustomAttribute(ConstructorInfo*, unsigned char __gc[]);

[JScript] public function SetCustomAttribute(ConstructorInfo, Byte[]);

使用例

[Visual Basic, C#, C++] ConstructorBuilder のコンテキストでバイト BLOB を渡す、 SetCustomAttribute の使用方法については、次のコード例を参照してください。

[Visual Basic, C#, C++] メモ   ここでは、SetCustomAttribute のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

<AttributeUsage(AttributeTargets.All, AllowMultiple := False)>  _
Public Class MyAttribute
   Inherits Attribute
   Public myBoolean As Boolean

   Public Sub New(myBoolean As Boolean)
      Me.myBoolean = myBoolean
   End Sub 'New
End Class 'MyAttribute

Public Class MyConstructorBuilder

   Public Shared Sub Main()
      Dim myHelloworld As Type = MyCreateCallee(Thread.GetDomain())
      Dim myConstructor As ConstructorInfo = myHelloworld.GetConstructor(New Type() _
                                                                        {GetType(String)})
      Dim myAttributes1 As Object() = myConstructor.GetCustomAttributes(True)
      Console.WriteLine("MyAttribute custom attribute contains  ")
      Dim index As Integer
      For index = 0 To myAttributes1.Length - 1
         If TypeOf myAttributes1(index) Is MyAttribute Then
            Console.WriteLine("myBoolean : " + _
                               CType(myAttributes1(index), MyAttribute).myBoolean.ToString())
         End If
      Next index
   End Sub 'Main

   Private Shared Function MyCreateCallee(domain As AppDomain) As Type
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "EmittedAssembly"
      ' Define a dynamic assembly in the current application domain.
      Dim myAssembly As AssemblyBuilder = domain.DefineDynamicAssembly _
                                       (myAssemblyName, AssemblyBuilderAccess.Run)
      ' Define a dynamic module in this assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Construct a 'TypeBuilder' given the name and attributes.
      Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType _
                                                       ("HelloWorld", TypeAttributes.Public)
      ' Define a constructor of the dynamic class.
      Dim myConstructor As ConstructorBuilder = myTypeBuilder.DefineConstructor _
                (MethodAttributes.Public, CallingConventions.Standard, New Type() {GetType(String)})
      Dim myILGenerator As ILGenerator = myConstructor.GetILGenerator()
      myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked")
      myILGenerator.Emit(OpCodes.Ldarg_1)
      Dim myMethodInfo As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() _
                                                                          {GetType(String)})
      myILGenerator.Emit(OpCodes.Call, myMethodInfo)
      myILGenerator.Emit(OpCodes.Ret)
      Dim myType As Type = GetType(MyAttribute)
      Dim myConstructorInfo As ConstructorInfo = myType.GetConstructor(New Type() {GetType(Boolean)})
      Try
         myConstructor.SetCustomAttribute(myConstructorInfo, New Byte() {1, 0, 1})
      Catch ex As ArgumentNullException
         Console.WriteLine("The following exception has occured : " + ex.Message)
      Catch ex As Exception
         Console.WriteLine("The following exception has occured : " + ex.Message)
      End Try
      Return myTypeBuilder.CreateType()
   End Function 'MyCreateCallee
End Class 'MyConstructorBuilder

[C#] 
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;


[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
   public bool myBoolean;

   public MyAttribute(bool myBoolean)
   {
      this.myBoolean = myBoolean;
   }
}

public class MyConstructorBuilder
{
   public static void Main()
   {
      Type myHelloworld = MyCreateCallee(Thread.GetDomain());
      ConstructorInfo myConstructor = myHelloworld.GetConstructor(new Type[]{typeof(String)});
      object[] myAttributes1 = myConstructor.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains  ");
      for(int index=0; index < myAttributes1.Length; index++)
      {
         if(myAttributes1[index] is MyAttribute)
         {
            Console.WriteLine("myBoolean : " + ((MyAttribute)myAttributes1[index]).myBoolean);
         }
      }
   }

   private static Type MyCreateCallee(AppDomain domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder myAssembly =
                  domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // Define a dynamic module in this assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
      // Construct a 'TypeBuilder' given the name and attributes.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
         TypeAttributes.Public);
      // Define a constructor of the dynamic class.
      ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
               MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)});
      ILGenerator myILGenerator = myConstructor.GetILGenerator();
      myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
      myILGenerator.Emit(OpCodes.Ldarg_1);
      MethodInfo myMethodInfo =
                     typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)});
      myILGenerator.Emit(OpCodes.Call, myMethodInfo);
      myILGenerator.Emit(OpCodes.Ret);
      Type myType = typeof(MyAttribute);
      ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[]{typeof(bool)});
      try
      {
         myConstructor.SetCustomAttribute(myConstructorInfo, new byte[]{01,00,01});
      }
      catch(ArgumentNullException ex)
      {
         Console.WriteLine("The following exception has occured : "+ex.Message);
      }
      catch(Exception ex)
      {
         Console.WriteLine("The following exception has occured : "+ex.Message);
      }
      return myTypeBuilder.CreateType();
   }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

[AttributeUsage(AttributeTargets::All, AllowMultiple = false)]
public __gc class MyAttribute : public Attribute 
{
public:
   bool myBoolean;

   MyAttribute(bool myBoolean) {
      this->myBoolean = myBoolean;
   }
};

static Type* MyCreateCallee(AppDomain* domain) 
{
   AssemblyName* myAssemblyName = new AssemblyName();
   myAssemblyName->Name = S"EmittedAssembly";
   // Define a dynamic assembly in the current application domain.
   AssemblyBuilder* myAssembly =
      domain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);
   // Define a dynamic module in this assembly.
   ModuleBuilder* myModuleBuilder = myAssembly->DefineDynamicModule(S"EmittedModule");
   // Construct a 'TypeBuilder' given the name and attributes.
   TypeBuilder* myTypeBuilder = myModuleBuilder->DefineType(S"HelloWorld",
      TypeAttributes::Public);
   // Define a constructor of the dynamic class.
   Type* type1[] = {__typeof(String)};
   ConstructorBuilder* myConstructor =
      myTypeBuilder->DefineConstructor(MethodAttributes::Public, CallingConventions::Standard, type1);
   ILGenerator* myILGenerator = myConstructor->GetILGenerator();
   myILGenerator->Emit(OpCodes::Ldstr, S"Constructor is invoked");
   myILGenerator->Emit(OpCodes::Ldarg_1);
   Type* type2[] = {__typeof(String)};
   MethodInfo* myMethodInfo =
      __typeof(Console)->GetMethod(S"WriteLine", type2);
   myILGenerator->Emit(OpCodes::Call, myMethodInfo);
   myILGenerator->Emit(OpCodes::Ret);
   Type* myType = __typeof(MyAttribute);
   Type* type3[] = {__typeof(bool)};
   ConstructorInfo* myConstructorInfo = myType->GetConstructor(type3);
   try {
      Byte bytes[] = {01, 00, 01};
      myConstructor->SetCustomAttribute(myConstructorInfo, bytes);
   } catch (ArgumentNullException* ex) {
      Console::WriteLine(S"The following exception has occured : {0}", ex->Message);
   } catch (Exception* ex) {
      Console::WriteLine(S"The following exception has occured : {0}", ex->Message);
   }
   return myTypeBuilder->CreateType();
}

void main()
{
   Type* myHelloworld = MyCreateCallee(Thread::GetDomain());
   Type* type1[] = {__typeof(String)};
   ConstructorInfo* myConstructor = myHelloworld->GetConstructor(type1);
   Object* myAttributes1[] = myConstructor->GetCustomAttributes(true);
   Console::WriteLine(S"MyAttribute custom attribute contains  ");
   for (int index=0; index < myAttributes1->Length; index++)
   {
      if (dynamic_cast<MyAttribute*>(myAttributes1[index])) 
      {
         Console::WriteLine(S"myBoolean : {0}", __box(__try_cast<MyAttribute*>(myAttributes1[index])->myBoolean));
      }
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

ConstructorBuilder クラス | ConstructorBuilder メンバ | System.Reflection.Emit 名前空間