This documentation is archived and is not being maintained.

AssemblyBuilder.SetCustomAttribute Method

Sets a custom attribute on this assembly.

Overload List

Set a custom attribute on this assembly using a custom attribute builder.

[Visual Basic] Overloads Public Sub SetCustomAttribute(CustomAttributeBuilder)
[C#] public void SetCustomAttribute(CustomAttributeBuilder);
[C++] public: void SetCustomAttribute(CustomAttributeBuilder*);
[JScript] public function SetCustomAttribute(CustomAttributeBuilder);

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

[Visual Basic] Overloads Public Sub SetCustomAttribute(ConstructorInfo, Byte())
[C#] public void SetCustomAttribute(ConstructorInfo, byte[]);
[C++] public: void SetCustomAttribute(ConstructorInfo*, unsigned char __gc[]);
[JScript] public function SetCustomAttribute(ConstructorInfo, Byte[]);

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of SetCustomAttribute to attach a custom attribute to a dynamically generated assembly.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SetCustomAttribute. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit


<AttributeUsage(AttributeTargets.All, AllowMultiple := False)>  _
Public Class MyAttribute
   Inherits Attribute
   Public s As Boolean
   
   Public Sub New(s As Boolean)
      Me.s = s
   End Sub 'New
End Class 'MyAttribute

Class MyApplication
   
   Public Shared Sub Main()
      Dim customAttribute As Type = CreateCallee(Thread.GetDomain())
      Dim attributes As Object() = customAttribute.Assembly.GetCustomAttributes(True)
      Console.WriteLine("MyAttribute custom attribute contains : ")
      Dim index As Integer
      For index = 0 To attributes.Length - 1
         If TypeOf attributes(index) Is MyAttribute Then
            Console.WriteLine("s : " + CType(attributes(index), MyAttribute).s.ToString())
            Exit For
         End If
      Next index
   End Sub 'Main
   
   Private Shared Function CreateCallee(domain As AppDomain) As Type
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "EmittedAssembly"
      Dim myAssembly As AssemblyBuilder = domain.DefineDynamicAssembly(myAssemblyName, _
                                                            AssemblyBuilderAccess.Run)
      Dim myType As Type = GetType(MyAttribute)
      Dim infoConstructor As ConstructorInfo = myType.GetConstructor(New Type() {GetType(Boolean)})
      myAssembly.SetCustomAttribute(infoConstructor, New Byte() {01, 00, 01})
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Define a public class named "HelloWorld" in the assembly.
      Dim helloWorldClass As TypeBuilder = myModule.DefineType("HelloWorld", TypeAttributes.Public)
      
      Return helloWorldClass.CreateType()
   End Function 'CreateCallee
End Class 'MyApplication

[C#] 
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);
            break;
         }
      }
   }

   private static Type CreateCallee(AppDomain domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(myAssemblyName,
         AssemblyBuilderAccess.Run);
      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);

      return(helloWorldClass.CreateType());
  }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

[AttributeUsage(AttributeTargets::All, AllowMultiple = false)]
public __gc class MyAttribute : public Attribute
{
public:
   bool s;
   MyAttribute(bool s)
   {
      this->s = s;
   }
};

Type* CreateCallee(AppDomain* domain)
{
   AssemblyName* myAssemblyName = new AssemblyName();
   myAssemblyName->Name = S"EmittedAssembly";
   AssemblyBuilder* myAssembly = domain->DefineDynamicAssembly(myAssemblyName,
      AssemblyBuilderAccess::Run);
   Type* myType = __typeof(MyAttribute);

   Type* temp0 [] = {__typeof(bool)};

   ConstructorInfo* infoConstructor = myType->GetConstructor(temp0);

   Byte temp1 [] = {01,00,01};

   myAssembly->SetCustomAttribute(infoConstructor, temp1);
   ModuleBuilder* myModule = myAssembly->DefineDynamicModule(S"EmittedModule");
   // Define a public class named "HelloWorld" in the assembly.
   TypeBuilder* helloWorldClass = myModule->DefineType(S"HelloWorld", TypeAttributes::Public);

   return(helloWorldClass->CreateType());
}

int main()
{
   Type* customAttribute = CreateCallee(Thread::GetDomain());
   Object* attributes[] = customAttribute->Assembly->GetCustomAttributes(true);
   Console::WriteLine(S"MyAttribute custom attribute contains : ");
   for(int index=0; index < attributes->Length; index++)
   {
      if(dynamic_cast<MyAttribute*>(attributes[index]))
      {
         Console::WriteLine(S"s : {0}", __box((dynamic_cast<MyAttribute*>(attributes[index]))->s));
         break;
      }
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

AssemblyBuilder Class | AssemblyBuilder Members | System.Reflection.Emit Namespace

Show: