This documentation is archived and is not being maintained.

EnumBuilder.SetCustomAttribute Method (CustomAttributeBuilder)

Sets a custom attribute using a custom attribute builder.

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

public void SetCustomAttribute (
	CustomAttributeBuilder customBuilder
)
public void SetCustomAttribute (
	CustomAttributeBuilder customBuilder
)
public function SetCustomAttribute (
	customBuilder : CustomAttributeBuilder
)

Parameters

customBuilder

An instance of a helper class to define the custom attribute.

Exception typeCondition

ArgumentNullException

con is a null reference (Nothing in Visual Basic).

The following code sample illustrates the use of SetCustomAttribute in the context of EnumBuilder, passing a CustomAttributeBuilder.

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()
   {
      try
      {
         CreateCallee(Thread.GetDomain());
         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: " + 
                                       ((MyAttribute)myAttributesArray[index]).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.CreateType();
      myEnumBuilder.SetCustomAttribute(myInfo, new byte[]{01,00,01});
      
   }
}

import System.*;
import System.Threading.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

/** @attribute AttributeUsage(AttributeTargets.All, AllowMultiple = false)
 */
public class MyAttribute extends Attribute
{
    public boolean myBoolValue;

    public MyAttribute(boolean myBool)
    {
        this.myBoolValue = myBool;
    } //MyAttribute
} //MyAttribute

class MyApplication
{
    private static EnumBuilder myEnumBuilder;

    public static void main(String[] args)
    {
        try {
            CreateCallee(System.Threading.Thread.GetDomain());

            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.get_Item(index) instanceof MyAttribute) {
                    Console.WriteLine("myBoolValue: " 
                        + (System.Boolean)((MyAttribute)myAttributesArray.
                        get_Item(index)).myBoolValue);
                }
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The following exception is raised:"
                + e.get_Message());
        }
    } //main

    private static void CreateCallee(AppDomain domain)
    {
        AssemblyName myAssemblyName = new AssemblyName();

        // Create a name for the assembly.
        myAssemblyName.set_Name("EmittedAssembly");

        // Create the dynamic assembly.
        AssemblyBuilder myAssemblyBuilder = domain.
            DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
        Type myType = MyAttribute.class.ToType();
        ConstructorInfo myInfo = myType.
            GetConstructor(new Type[] { boolean.class.ToType() });

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

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

        FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("FieldOne",
            (Int32)1);
        FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("FieldTwo",
            (Int32)2);
        myEnumBuilder.CreateType();
        myEnumBuilder.SetCustomAttribute(myInfo, new ubyte[] { 1, 0, 1 });
    } //CreateCallee 
} //MyApplication

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
Show: