This documentation is archived and is not being maintained.

AssemblyBuilder.SetCustomAttribute Method (ConstructorInfo, Byte[])

Set a custom attribute on this assembly using a specified custom attribute blob.

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

public void SetCustomAttribute(
	ConstructorInfo con,
	byte[] binaryAttribute


Type: System.Reflection.ConstructorInfo
The constructor for the custom attribute.
Type: System.Byte[]
A byte blob representing the attributes.


con or binaryAttribute is null.


The caller does not have the required permission.


con is not a RuntimeConstructorInfo.

See the metadata specification in the ECMA Partition II documentation for details on how to format binaryAttribute. The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

RuntimeConstructorInfo is a special type generated by the system. It derives from the ConstructorInfo class, and any ConstructorInfo object you obtain through reflection is actually an instance of RuntimeConstructorInfo. For information on runtime types, see Runtime Types in Reflection.


SetCustomAttribute cannot be used to set declarative security attributes. Use one of the overloads of DefineDynamicAssembly that takes required, optional, and refused permissions.


Starting with the .NET Framework version 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework version 3.5 or later.

The following code sample illustrates the use of SetCustomAttribute to attach a custom attribute to a dynamically generated assembly.

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

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

   public MyAttribute(bool s)
      this.s = s;

class MyApplication
   public static void Main()
      Type customAttribute = CreateCallee(Thread.GetDomain());
      object[] attributes = customAttribute.Assembly.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains : ");
      for(int index=0; index < attributes.Length; index++)
         if(attributes[index] is MyAttribute)
            Console.WriteLine("s : " + ((MyAttribute)attributes[index]).s);

   private static Type CreateCallee(AppDomain domain)
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(myAssemblyName,
      Type myType = typeof(MyAttribute);
      ConstructorInfo infoConstructor = myType.GetConstructor(new Type[]{typeof(bool)});
      myAssembly.SetCustomAttribute(infoConstructor, new byte[]{01,00,01});
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "HelloWorld" in the assembly.
      TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.