Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FieldBuilder::SetCustomAttribute Method (CustomAttributeBuilder^)

 

Sets a custom attribute 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.

InvalidOperationException

The parent type of this field is complete.

The following code sample illustrates the use of SetCustomAttribute in the context of FieldBuilder, using a CustomAttributeBuilder.

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

[AttributeUsage(AttributeTargets::All,AllowMultiple=false)]
public ref class MyAttribute1: public Attribute
{
public:
   String^ myCustomAttributeValue;
   MyAttribute1( String^ myString )
   {
      myCustomAttributeValue = myString;
   }
};


[AttributeUsage(AttributeTargets::All,AllowMultiple=false)]
public ref class MyAttribute2: public Attribute
{
public:
   bool myCustomAttributeValue;
   MyAttribute2( bool myBool )
   {
      myCustomAttributeValue = myBool;
   }
};

Type^ CreateCallee( AppDomain^ currentDomain )
{
   // Create a simple name for the assembly.
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "EmittedAssembly";

   // Create the called dynamic assembly.
   AssemblyBuilder^ myAssemblyBuilder = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::RunAndSave );
   ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "EmittedModule", "EmittedModule.mod" );

   // Define a public class named 'CustomClass' in the assembly.
   TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType( "CustomClass", TypeAttributes::Public );

   // Define a private String field named 'MyField' in the type.
   FieldBuilder^ myFieldBuilder = myTypeBuilder->DefineField( "MyField", String::typeid, FieldAttributes::Public );
   Type^ myAttributeType1 = MyAttribute1::typeid;

   // Create a Constructorinfo Object* for attribute 'MyAttribute1'.
   array<Type^>^type1 = {String::typeid};
   ConstructorInfo^ myConstructorInfo = myAttributeType1->GetConstructor( type1 );

   // Create the CustomAttribute instance of attribute of type 'MyAttribute1'.
   array<Object^>^obj1 = {"Test"};
   CustomAttributeBuilder^ attributeBuilder = gcnew CustomAttributeBuilder( myConstructorInfo,obj1 );

   // Set the CustomAttribute 'MyAttribute1' to the Field.
   myFieldBuilder->SetCustomAttribute( attributeBuilder );
   Type^ myAttributeType2 = MyAttribute2::typeid;

   // Create a Constructorinfo Object* for attribute 'MyAttribute2'.
   array<Type^>^type2 = {bool::typeid};
   ConstructorInfo^ myConstructorInfo2 = myAttributeType2->GetConstructor( type2 );

   // Set the CustomAttribute 'MyAttribute2' to the Field.
   array<Byte>^bytes = {01,00,01,00,00};
   myFieldBuilder->SetCustomAttribute( myConstructorInfo2, bytes );

   // Create a method.
   array<Type^>^type3 = {String::typeid,int::typeid};
   MethodBuilder^ myMethodBuilder = myTypeBuilder->DefineMethod( "MyMethod", MethodAttributes::Public, nullptr, type3 );
   ILGenerator^ myILGenerator = myMethodBuilder->GetILGenerator();
   myILGenerator->Emit( OpCodes::Ldarg_0 );
   myILGenerator->Emit( OpCodes::Ldarg_1 );
   myILGenerator->Emit( OpCodes::Stfld, myFieldBuilder );
   myILGenerator->EmitWriteLine( "Value of the Field is :" );
   myILGenerator->EmitWriteLine( myFieldBuilder );
   myILGenerator->Emit( OpCodes::Ret );
   return myTypeBuilder->CreateType();
}

int main()
{
   try
   {
      Type^ myCustomClass = CreateCallee( Thread::GetDomain() );

      // Construct an instance of a type.
      Object^ myObject = Activator::CreateInstance( myCustomClass );
      Console::WriteLine( "FieldBuilder Sample" );

      // Find a method in this type and call it on this Object*.
      MethodInfo^ myMethodInfo = myCustomClass->GetMethod( "MyMethod" );
      array<Object^>^obj1 = {"Sample string",3};
      myMethodInfo->Invoke( myObject, obj1 );

      // Retrieve the values of Attributes applied to field and display to console.
      array<FieldInfo^>^myFieldInfo = myCustomClass->GetFields();
      for ( int i = 0; i < myFieldInfo->Length; i++ )
      {
         array<Object^>^attributes = myFieldInfo[ i ]->GetCustomAttributes( true );
         for ( int index = 0; index < attributes->Length; index++ )
         {
            if ( dynamic_cast<MyAttribute1^>(attributes[ index ]) )
            {
               MyAttribute1^ myCustomAttribute = safe_cast<MyAttribute1^>(attributes[ index ]);
               Console::WriteLine( "Attribute Value of (MyAttribute1): {0}", myCustomAttribute->myCustomAttributeValue );
            }
            if ( dynamic_cast<MyAttribute2^>(attributes[ index ]) )
            {
               MyAttribute2^ myCustomAttribute = safe_cast<MyAttribute2^>(attributes[ index ]);
               Console::WriteLine( "Attribute Value of (MyAttribute2): {0}", myCustomAttribute->myCustomAttributeValue );
            }
         }
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception Caught {0}", e->Message );
   }
}

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