次の方法で共有


EnumBuilder.GetCustomAttributes メソッド (Type, Boolean)

指定された型によって識別されるカスタム属性を返します。

Overrides Overloads Public Function GetCustomAttributes( _
   ByVal attributeType As Type, _   ByVal inherit As Boolean _) As Object() Implements ICustomAttributeProvider.GetCustomAttributes
[C#]
public override object[] GetCustomAttributes(TypeattributeType,boolinherit);
[C++]
public: Object* GetCustomAttributes(Type* attributeType,boolinherit)  __gc[];
[JScript]
public override function GetCustomAttributes(
   attributeType : Type,inherit : Boolean) : Object[];

パラメータ

  • attributeType
    カスタム属性を適用する対象の Type オブジェクト。
  • inherit
    このメンバの継承チェインを検索して属性を見つけるかどうかを指定します。

戻り値

このコンストラクタの属性を表すオブジェクトの配列を返します。コンストラクタの属性は、 Type attributeType です。

実装

ICustomAttributeProvider.GetCustomAttributes

例外

例外の種類 条件
NotSupportedException このメソッドは現在、不完全な型に対してはサポートされていません。

解説

終了型のカスタム属性を取得する代替手段としては、 Type.GetType を使用して型を取得し、返された Type に対して MemberInfo.GetCustomAttributes を呼び出します。

次のコード例は、 EnumBuilder のコンテキストで GetCustomAttribute を使用する方法を示しています。

 
<AttributeUsage(AttributeTargets.All, AllowMultiple := False)>  _
Public Class MyAttribute
   Inherits Attribute
   Public myString As String
   Public myInteger As Integer

   Public Sub New(myString1 As String, myInteger1 As Integer)
      Me.myString = myString1
      Me.myInteger = myInteger1
   End Sub 'New
End Class 'MyAttribute

Class MyApplication
   Private Shared myAssemblyBuilder As AssemblyBuilder
   Private Shared myEnumBuilder As EnumBuilder

   Public Shared Sub Main()
      Try
         CreateCallee(Thread.GetDomain())
         If myEnumBuilder.IsDefined(GetType(MyAttribute), False) Then
            Dim myAttributesArray As Object() = myEnumBuilder.GetCustomAttributes _
                                                      (GetType(MyAttribute), False)
            Console.WriteLine("Custom attribute contains: ")
            ' Read the attributes and display them on the console.
            Dim index As Integer
            For index = 0 To myAttributesArray.Length - 1
               If TypeOf myAttributesArray(index) Is MyAttribute Then
                  Console.WriteLine("The value of myString  is: " + CType(myAttributesArray(index), _
                                                                           MyAttribute).myString)
                  Console.WriteLine("The value of myInteger is: " + CType(myAttributesArray(index), _
                                                               MyAttribute).myInteger.ToString())
               End If
            Next index
         Else
            Console.WriteLine("Custom Attributes are not set for the EnumBuilder")
         End If
      Catch e As Exception
         Console.WriteLine("The following exception is raised:" + e.Message)
      End Try
   End Sub 'Main

   Private Shared Sub CreateCallee(___domain As AppDomain)
      ' Create a name for the assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "EmittedAssembly"

      ' Create the dynamic assembly.
      myAssemblyBuilder = ___domain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)

      Dim myType As Type = GetType(MyAttribute)
      Dim myInfo As ConstructorInfo = myType.GetConstructor(New Type(1) {GetType(String), _
                                                                           GetType(Integer)})
      Dim myCustomAttributeBuilder As New CustomAttributeBuilder(myInfo, New Object(1) {"Hello", 2})

      ' Create a dynamic module.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule("EmittedModule")

      ' Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum", TypeAttributes.Public, _
                                                                                 GetType(Int32))

      Dim myFieldBuilder1 As FieldBuilder = myEnumBuilder.DefineLiteral("FieldOne", 1)
      Dim myFieldBuilder2 As FieldBuilder = myEnumBuilder.DefineLiteral("FieldTwo", 2)

      myEnumBuilder.CreateType()
      myEnumBuilder.SetCustomAttribute(myCustomAttributeBuilder)
   End Sub 'CreateCallee
End Class 'MyApplication

[C#] 
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
   public String myString;
   public int myInteger;

   public MyAttribute(String myString1, int myInteger1)
   {
      this.myString = myString1;
      this.myInteger = myInteger1;
   }
}

class MyApplication
{
   static AssemblyBuilder myAssemblyBuilder;
   static EnumBuilder myEnumBuilder;

   public static void Main()
   {
      try
      {
         CreateCallee(Thread.GetDomain());
         if(myEnumBuilder.IsDefined(typeof(MyAttribute),false))
         {
            object[] myAttributesArray = myEnumBuilder.GetCustomAttributes(typeof(MyAttribute),false);
            Console.WriteLine("Custom attribute contains: ");
            // Read the attributes and display them on the console.
            for(int index=0; index < myAttributesArray.Length; index++)
            {
               if(myAttributesArray[index] is MyAttribute)
               {
                  Console.WriteLine("The value of myString  is: "
                                    + ((MyAttribute)myAttributesArray[index]).myString);
                  Console.WriteLine("The value of myInteger is: "
                                    + ((MyAttribute)myAttributesArray[index]).myInteger);
               }
            }
         }
         else
         {
            Console.WriteLine("Custom Attributes are not set for the EnumBuilder");
         }
      }
      catch(Exception e)
      {
         Console.WriteLine("The following exception is raised:" +e.Message);
      }

  }

   private static void CreateCallee(AppDomain ___domain)
   {
      // Create a name for the assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";

      // Create the dynamic assembly.
      myAssemblyBuilder = ___domain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);

      Type myType = typeof(MyAttribute);
      ConstructorInfo myInfo = myType.GetConstructor(new Type[2]{typeof(String), typeof(int)});
      CustomAttributeBuilder myCustomAttributeBuilder =
                                    new CustomAttributeBuilder(myInfo, new object[2]{"Hello", 2});

      // Create a dynamic module.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("EmittedModule");

      // Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum", TypeAttributes.Public, typeof(Int32));

      FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("FieldOne", 1);
      FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("FieldTwo", 2);

      myEnumBuilder.CreateType();
      myEnumBuilder.SetCustomAttribute(myCustomAttributeBuilder);
   }
}

[C++] 
[AttributeUsage(AttributeTargets::All, AllowMultiple = false)]
public __gc class MyAttribute : public Attribute
{
public:
   String* myString;
   int myInteger;

   MyAttribute(String* myString1, int myInteger1)
   {
      this->myString = myString1;
      this->myInteger = myInteger1;
   }
};

__gc class MyApplication
{
   static AssemblyBuilder* myAssemblyBuilder;
   static EnumBuilder* myEnumBuilder;

public:
   static void Main()
   {
      try
      {
         CreateCallee(Thread::GetDomain());
         if(myEnumBuilder->IsDefined(__typeof(MyAttribute),false))
         {
            Object* myAttributesArray[] = myEnumBuilder->GetCustomAttributes(__typeof(MyAttribute),false);
            Console::WriteLine(S"Custom attribute contains: ");
            // Read the attributes and display them on the console.
            for(int index=0; index < myAttributesArray->Length; index++)
            {
               if(dynamic_cast<MyAttribute*>(myAttributesArray[index]))
               {
                  Console::WriteLine(S"The value of myString  is: {0}",
                     (dynamic_cast<MyAttribute*>(myAttributesArray[index]))->myString);
                  Console::WriteLine(S"The value of myInteger is: {0}",
                     __box((dynamic_cast<MyAttribute*>(myAttributesArray[index]))->myInteger));
               }
            }
         }
         else
         {
            Console::WriteLine(S"Custom Attributes are not set for the EnumBuilder");
         }
      }
      catch(Exception* e)
      {
         Console::WriteLine(S"The following exception is raised:{0}", e->Message);
      }

   }

private:
   static void CreateCallee(AppDomain* ___domain)
   {
      // Create a name for the assembly.
      AssemblyName* myAssemblyName = new AssemblyName();
      myAssemblyName->Name = S"EmittedAssembly";

      // Create the dynamic assembly.
      myAssemblyBuilder = ___domain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);

      Type* myType = __typeof(MyAttribute);

      Type* temp0 [] = {__typeof(String), __typeof(int)};
      ConstructorInfo* myInfo = myType->GetConstructor(temp0);
      Object* temp1 [] = {S"Hello", __box(2)};
      CustomAttributeBuilder* myCustomAttributeBuilder = new CustomAttributeBuilder(myInfo, temp1);

      // Create a dynamic module.
      ModuleBuilder* myModuleBuilder = myAssemblyBuilder->DefineDynamicModule(S"EmittedModule");

      // Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder->DefineEnum(S"MyNamespace.MyEnum", TypeAttributes::Public, __typeof(Int32));

      FieldBuilder* myFieldBuilder1 = myEnumBuilder->DefineLiteral(S"FieldOne",__box(1));
      FieldBuilder* myFieldBuilder2 = myEnumBuilder->DefineLiteral(S"FieldTwo",__box(2));

      myEnumBuilder->CreateType();
      myEnumBuilder->SetCustomAttribute(myCustomAttributeBuilder);
   }
};

int main()
{
   MyApplication::Main();
}

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

EnumBuilder クラス | EnumBuilder メンバ | System.Reflection.Emit 名前空間 | EnumBuilder.GetCustomAttributes オーバーロードの一覧