Share via


AssemblyBuilder.SetCustomAttribute Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sets a custom attribute on this assembly by using a custom attribute builder.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub SetCustomAttribute ( _
    customBuilder As CustomAttributeBuilder _
)
[SecuritySafeCriticalAttribute]
public void SetCustomAttribute(
    CustomAttributeBuilder customBuilder
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

customBuilder is nulla null reference (Nothing in Visual Basic).

Examples

The following example shows how to use the SetCustomAttribute(CustomAttributeBuilder) method overload to add attributes to a dynamic assembly. The example defines an attribute named MyAttribute, and applies MyAttribute and AssemblyVersionAttribute to the assembly by using CustomAttributeBuilder and SetCustomAttribute(CustomAttributeBuilder).

Imports System.Reflection
Imports System.Reflection.Emit

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim aName As New AssemblyName("EmittedAssembly")
      Dim ab As AssemblyBuilder = _
         AppDomain.CurrentDomain.DefineDynamicAssembly(aName, _
                                                       AssemblyBuilderAccess.Run)
      Dim mb As ModuleBuilder = ab.DefineDynamicModule("EmittedModule")

      ' To apply the MyAttribute attribute to the assembly, first get the
      ' constructor for the attribute. The constructor for MyAttribute takes
      ' a string and an integer.
      Dim myAttrCtor As ConstructorInfo = _
         GetType(MyAttribute).GetConstructor(New Type() { GetType(String), GetType(Integer) })

      ' Next create a custom attribute builder for MyAttribute, using the 
      ' constructor and an array of arguments for the constructor.
      Dim myAttr As New CustomAttributeBuilder(myAttrCtor, New Object() { "Hello", 42 })

      ' Finally, apply the attribute.
      ab.SetCustomAttribute(myAttr)

      ' Similarly, apply an assembly version attribute.
      Dim infoAttrCtor As ConstructorInfo = _
         GetType(AssemblyVersionAttribute).GetConstructor(New Type() { GetType(String) })
      Dim infoAttr As New CustomAttributeBuilder(infoAttrCtor, New Object() { "4.0.2.0" })
      ab.SetCustomAttribute(infoAttr)

      ' Define a public class named "HelloWorld" in the assembly.
      Dim helloWorld As TypeBuilder = mb.DefineType("HelloWorld", TypeAttributes.Public)

      ' Create the type, and use it to list the assembly attributes.
      Dim myType As Type = helloWorld.CreateType()

      outputBlock.Text &= "Assembly attributes: " & vbCrLf
      Dim attributes As Object() = myType.Assembly.GetCustomAttributes(True)
      For Each attr As Object In attributes

         If TypeOf attr Is MyAttribute Then
            Dim ma As MyAttribute = CType(attr, MyAttribute)
            outputBlock.Text &= String.Format("MyAttribute(""{0}"", {1})" & vbLf, ma.S, ma.X)
         ElseIf TypeOf attr Is AssemblyVersionAttribute Then
            Dim ava As AssemblyVersionAttribute = CType(attr, AssemblyVersionAttribute)
            outputBlock.Text &= String.Format("AssemblyVersionAttribute(""{0}"")" & vbLf, _
                ava.Version)
         End If

      Next 

   End Sub 
End Class 

<AttributeUsage(AttributeTargets.All, AllowMultiple:=False)> _
Public Class MyAttribute
   Inherits Attribute

   Private sValue As String
   Public ReadOnly Property S As String
      Get 
         Return sValue
      End Get
   End Property

   Private xValue As Integer
   Public ReadOnly Property X As Integer
      Get 
         Return xValue
      End Get
   End Property

   Public Sub New(ByVal s As String, ByVal x As Integer)
      sValue = s
      xValue = x
   End Sub 
End Class 

' This example produces the following output:
'
'Assembly attributes:
'MyAttribute("Hello", 42)
'AssemblyVersionAttribute("4.0.2.0")
using System;
using System.Reflection;
using System.Reflection.Emit;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      AssemblyName aName = new AssemblyName("EmittedAssembly");
      AssemblyBuilder ab = 
         AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
      ModuleBuilder mb = ab.DefineDynamicModule("EmittedModule");

      // To apply the MyAttribute attribute to the assembly, first get the
      // constructor for the attribute. The constructor for MyAttribute takes
      // a string and an integer.
      ConstructorInfo myAttrCtor = 
         typeof(MyAttribute).GetConstructor(new Type[] { typeof(string), typeof(int) });

      // Next create a custom attribute builder for MyAttribute, using the 
      // constructor and an array of arguments for the constructor.
      CustomAttributeBuilder myAttr = 
         new CustomAttributeBuilder(myAttrCtor, new object[]{"Hello", 42});

      // Finally, apply the attribute.
      ab.SetCustomAttribute(myAttr);

      // Similarly, apply an assembly version attribute.
      ConstructorInfo infoAttrCtor = 
         typeof(AssemblyVersionAttribute).GetConstructor(new Type[] { typeof(string) });
      CustomAttributeBuilder infoAttr = 
         new CustomAttributeBuilder(infoAttrCtor, new object[] { "4.0.2.0" });
      ab.SetCustomAttribute(infoAttr);

      // Define a public class named "HelloWorld" in the assembly.
      TypeBuilder helloWorld = mb.DefineType("HelloWorld", TypeAttributes.Public);

      // Create the type, and use it to list the assembly attributes.
      Type myType = helloWorld.CreateType();

      outputBlock.Text += "Assembly attributes: \r\n";
      object[] attributes = myType.Assembly.GetCustomAttributes(true);
      foreach( object attr in attributes )
      {
         if (attr is MyAttribute)
         {
            MyAttribute ma = (MyAttribute) attr;
            outputBlock.Text += String.Format("MyAttribute(\"{0}\", {1})\n", ma.S, ma.X);
         }
         else if(attr is AssemblyVersionAttribute)
         {
            AssemblyVersionAttribute ava = (AssemblyVersionAttribute) attr;
            outputBlock.Text += 
               String.Format("AssemblyVersionAttribute(\"{0}\")\n", ava.Version);
         }
      }
   }
}

[AttributeUsage(AttributeTargets.All, AllowMultiple=false)]
public class MyAttribute : Attribute
{
   private string sValue;
   public string S { get { return sValue; }}

   public int xValue;
   public int X { get { return xValue; }}

   public MyAttribute(string s, int x)
   {
      sValue = s;
      xValue = x;
   }
}

/* This example produces the following output:

Assembly attributes:
MyAttribute("Hello", 42)
AssemblyVersionAttribute("4.0.2.0")
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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