EnumBuilder.GetCustomAttributes Method (Boolean)


Returns all the custom attributes defined for this constructor.

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

public override object[] GetCustomAttributes(
	bool inherit


Type: System.Boolean

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

Return Value

Type: System.Object[]

Returns an array of objects representing all the custom attributes of the constructor represented by this ConstructorBuilder instance.

Exception Condition

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 GetCustomAttributes on the returned Type.

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

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

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

   public MyAttribute(bool myBool)
      this.myBoolValue = myBool;

class MyApplication
   static EnumBuilder myEnumBuilder;

   public static void Main()
         object[] myAttributesArray = myEnumBuilder.GetCustomAttributes(true);

         // Read the attributes and display them on the console.
         Console.WriteLine("Custom attribute contains: ");
         for(int index=0; index < myAttributesArray.Length; index++)
            if(myAttributesArray[index] is MyAttribute)
               Console.WriteLine("myBoolValue: " + 
      catch(Exception e)
         Console.WriteLine("The following exception is raised:" +e.Message);

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

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

      Type myType = typeof(MyAttribute);
      ConstructorInfo myInfo = myType.GetConstructor(new Type[]{typeof(bool)});

      // 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.SetCustomAttribute(myInfo, new byte[]{01,00,01});


.NET Framework
Available since 1.1
Available since 2.0
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft