Export (0) Print
Expand All

EnumBuilder.GetCustomAttributes Method (Type, Boolean)

Returns the custom attributes identified by the given type.

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

public override Object[] GetCustomAttributes (
	Type attributeType,
	bool inherit
public Object[] GetCustomAttributes (
	Type attributeType, 
	boolean inherit
public override function GetCustomAttributes (
	attributeType : Type, 
	inherit : boolean
) : Object[]



The Type object to which the custom attributes are applied.


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

Return Value

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

Exception typeCondition


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 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()
            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);
            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);


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

© 2014 Microsoft