EnumBuilder.SetCustomAttribute Method

Sets a custom attribute using a custom attribute builder.

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

public void SetCustomAttribute(
	CustomAttributeBuilder customBuilder


Type: System.Reflection.Emit.CustomAttributeBuilder
An instance of a helper class to define the custom attribute.


con is null.

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

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

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

   public bool MyBool { get { return myBoolValue; }}   

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

class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // Get the current application domain.
      AppDomain currentDomain = AppDomain.CurrentDomain;

      // Create a dynamic assembly in the current application domain, 
      // and allow it to be executed.
      AssemblyName aName = new AssemblyName("TempAssembly");
      AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
          aName, AssemblyBuilderAccess.Run);

      // Define a dynamic module in "TempAssembly" assembly. The module can
      // have the same name as the assembly.
      ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);

      // Define a public enumeration with the name "Elevation" and an 
      // underlying type of Integer.
      EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int));

      // Define two members, "High" and "Low".
      eb.DefineLiteral("Low", 0);
      eb.DefineLiteral("High", 1);

      // To set a custom attribute, first get the constructor for the attribute.
      // Use the constructor and an array of arguments for the constructor to
      // create a CustomAttributeBuilder. Finally, use the CustomAttributeBuilder
      // to set the attribute on the enumeration. 
      Type attrType = typeof(MyAttribute);
      ConstructorInfo attrCtor = 
         attrType.GetConstructor(new Type[] {typeof(bool)});
      CustomAttributeBuilder attrBuilder =
         new CustomAttributeBuilder(attrCtor, new Object[] { true });

      // Create the type.
      Type finished = eb.CreateType();

      foreach (FieldInfo fi in finished.GetFields())
         outputBlock.Text += String.Format("{0}.{1} = {2}\n", 
            finished.Name, fi.Name, fi.GetRawConstantValue());

      // Read the attributes and display them.
      outputBlock.Text += "\nCustom attributes: \n";
      foreach (object attr in finished.GetCustomAttributes(true))
         outputBlock.Text += attr.ToString() + "\n";
         if (attr is MyAttribute)
            outputBlock.Text += String.Format("    MyBool: {0}\n",
               ((MyAttribute) attr).MyBool.ToString());

/* This code example produces the following output:

Elevation.Low = 0
Elevation.High = 1 

Custom attributes:
    MyBool: True


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions