AssemblyBuilder::SetCustomAttribute Method (ConstructorInfo^, array<Byte>^)

 

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

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

public:
[ComVisibleAttribute(true)]
void SetCustomAttribute(
	ConstructorInfo^ con,
	array<unsigned char>^ binaryAttribute
)

Parameters

con
Type: System.Reflection::ConstructorInfo^

The constructor for the custom attribute.

binaryAttribute
Type: array<System::Byte>^

A byte blob representing the attributes.

Exception Condition
ArgumentNullException

con or binaryAttribute is null.

SecurityException

The caller does not have the required permission.

ArgumentException

con is not a RuntimeConstructorInfo object.

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.

System_CAPS_noteNote

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

System_CAPS_noteNote

Starting with the .NET Framework 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 3.5 or later.

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

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

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

Type^ CreateCallee( AppDomain^ domain )
{
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "EmittedAssembly";
   AssemblyBuilder^ myAssembly = domain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );
   Type^ myType = MyAttribute::typeid;
   array<Type^>^temp0 = {bool::typeid};
   ConstructorInfo^ infoConstructor = myType->GetConstructor( temp0 );
   array<Byte>^temp1 = {01,00,01};
   myAssembly->SetCustomAttribute( infoConstructor, temp1 );
   ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule" );

   // Define a public class named "HelloWorld" in the assembly.
   TypeBuilder^ helloWorldClass = myModule->DefineType( "HelloWorld", TypeAttributes::Public );
   return (helloWorldClass->CreateType());
}

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

ReflectionPermission

when invoked late-bound through mechanisms such as Type::InvokeMember. Associated enumeration: ReflectionPermissionFlag::MemberAccess.

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show: