EnumBuilder::GetCustomAttributes Method (Type^, Boolean)

 

Returns the custom attributes identified by the given type.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public:
virtual array<Object^>^ GetCustomAttributes(
	Type^ attributeType,
	bool inherit
) override

Parameters

attributeType
Type: System::Type^

The Type object to which the custom attributes are applied.

inherit
Type: System::Boolean

Specifies whether to search this member's inheritance chain to find the attributes.

Return Value

Type: array<System::Object^>^

Returns an array of objects representing the attributes of this constructor that are of TypeattributeType.

Exception Condition
NotSupportedException

This method is not currently supported in types that are not complete.

As a workaround, to retrieve the custom attributes of a finished type, retrieve the type using Type::GetType and call MemberInfo::GetCustomAttributes on the returned Type.

The following code sample illustrates the use of GetCustomAttribute in the context of EnumBuilder.


[AttributeUsage(AttributeTargets::All,AllowMultiple=false)]
public ref class MyAttribute: public Attribute
{
public:
   String^ myString;
   int myInteger;
   MyAttribute( String^ myString1, int myInteger1 )
   {
      this->myString = myString1;
      this->myInteger = myInteger1;
   }

};

ref class MyApplication
{
private:
   static AssemblyBuilder^ myAssemblyBuilder;
   static EnumBuilder^ myEnumBuilder;

public:
   static void Main()
   {
      try
      {
         CreateCallee( Thread::GetDomain() );
         if ( myEnumBuilder->IsDefined( MyAttribute::typeid, false ) )
         {
            array<Object^>^myAttributesArray = myEnumBuilder->GetCustomAttributes( MyAttribute::typeid, false );
            Console::WriteLine( "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( "The value of myString  is: {0}", (dynamic_cast<MyAttribute^>(myAttributesArray[ index ]))->myString );
                  Console::WriteLine( "The value of myInteger is: {0}", (dynamic_cast<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:{0}", e->Message );
      }

   }

private:
   static void CreateCallee( AppDomain^ domain )
   {
      // Create a name for the assembly.
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "EmittedAssembly";

      // Create the dynamic assembly.
      myAssemblyBuilder = domain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );
      Type^ myType = MyAttribute::typeid;
      array<Type^>^temp0 = {String::typeid,int::typeid};
      ConstructorInfo^ myInfo = myType->GetConstructor( temp0 );
      array<Object^>^temp1 = {"Hello",2};
      CustomAttributeBuilder^ myCustomAttributeBuilder = gcnew CustomAttributeBuilder( myInfo,temp1 );

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

      // Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder->DefineEnum( "MyNamespace.MyEnum", TypeAttributes::Public, Int32::typeid );
      FieldBuilder^ myFieldBuilder1 = myEnumBuilder->DefineLiteral( "FieldOne", 1 );
      FieldBuilder^ myFieldBuilder2 = myEnumBuilder->DefineLiteral( "FieldTwo", 2 );
      myEnumBuilder->CreateType();
      myEnumBuilder->SetCustomAttribute( myCustomAttributeBuilder );
   }
};

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

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show: