AssemblyBuilder::SetCustomAttribute Method (CustomAttributeBuilder^)

 

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

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

public:
void SetCustomAttribute(
	CustomAttributeBuilder^ customBuilder
)

Parameters

customBuilder
Type: System.Reflection.Emit::CustomAttributeBuilder^

An instance of a helper class to define the custom attribute.

Exception Condition
ArgumentNullException

con is null.

SecurityException

The caller does not have the required permission.

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 within AssemblyBuilder, using a CustomAttributeBuilder.

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

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 = {String::typeid,int::typeid};
   ConstructorInfo^ infoConstructor = myType->GetConstructor( temp0 );
   array<Object^>^temp1 = {"Hello",2};
   CustomAttributeBuilder^ attributeBuilder = gcnew CustomAttributeBuilder( infoConstructor,temp1 );
   myAssembly->SetCustomAttribute( attributeBuilder );
   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 );
         Console::WriteLine( "x : {0}", (dynamic_cast<MyAttribute^>(attributes[ index ]))->x );
         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: