SetCustomAttribute Method
Collapse the table of content
Expand the table of content

FieldBuilder.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 parent type of this field is complete.

The following code sample illustrates the use of SetCustomAttribute in the context of FieldBuilder, using a CustomAttributeBuilder.

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

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

    public string MyString { get { return myStringValue; }}   

    public MyAttribute(string myString)
        myStringValue = myString;

class Example
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
        // Create a simple name for the assembly.
        AssemblyName myAssemblyName = new AssemblyName("EmittedAssembly");
        // Create the dynamic assembly.
        AssemblyBuilder myAssemblyBuilder =
                myAssemblyName, AssemblyBuilderAccess.Run);
        ModuleBuilder myModuleBuilder =
        // Define a public class named 'CustomClass' in the assembly.
        TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("CustomClass",

        // Define a private String field named 'MyField' in the type.
        FieldBuilder myFieldBuilder =
            myTypeBuilder.DefineField("MyField", typeof(String), FieldAttributes.Public);

        // 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 myAttributeType = typeof(MyAttribute);
        ConstructorInfo myConstructorInfo = 
            myAttributeType.GetConstructor(new Type[] { typeof(string) });
        CustomAttributeBuilder attributeBuilder =
            new CustomAttributeBuilder(myConstructorInfo, new object[] { "Test" });

        Type myCustomClass = myTypeBuilder.CreateType();
        // Retrieve the values of Attributes applied to field and display to console.
        FieldInfo myFieldInfo = myCustomClass.GetField("MyField");
        foreach (object attr in myFieldInfo.GetCustomAttributes(true))
            outputBlock.Text += attr.ToString() + "\n";
            if (attr is MyAttribute)
                outputBlock.Text += String.Format("    MyString: {0}\n",
                    ((MyAttribute) attr).MyString.ToString());

/* This example produces output similar to the following:

    MyString: Test


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

© 2016 Microsoft