Export (0) Print
Expand All

FieldBuilder Class

Defines and represents a field. This class cannot be inherited.

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

[ClassInterfaceAttribute(ClassInterfaceType::None)] 
[ComVisibleAttribute(true)] 
public ref class FieldBuilder sealed : public FieldInfo, _FieldBuilder
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class FieldBuilder extends FieldInfo implements _FieldBuilder
ClassInterfaceAttribute(ClassInterfaceType.None) 
ComVisibleAttribute(true) 
public final class FieldBuilder extends FieldInfo implements _FieldBuilder

NoteNote

The HostProtectionAttribute attribute applied to this class has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Get an instance of FieldBuilder by calling DefineField, DefineInitializedData, or DefineUninitializedData.

NoteNote

The SetValue method is currently not supported. As a workaround, retrieve the FieldInfo by reflecting on the finished type and call SetValue to set the value of the field.

The following code sample illustrates the use of FieldBuilder.

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ CreateType( AppDomain^ currentDomain )
{
   // Create an assembly.
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "DynamicAssembly";
   AssemblyBuilder^ myAssembly = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );

   // Create a dynamic module in Dynamic Assembly.
   ModuleBuilder^ myModuleBuilder = myAssembly->DefineDynamicModule( "MyModule" );

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

   // Define a private String field named S"MyField" in the type.
   FieldBuilder^ myFieldBuilder = myTypeBuilder->DefineField( "MyField", String::typeid, static_cast<FieldAttributes>(FieldAttributes::Private | FieldAttributes::Static) );

   // Create the constructor.
   array<Type^>^constructorArgs = {String::typeid};
   ConstructorBuilder^ constructor = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, constructorArgs );
   ILGenerator^ constructorIL = constructor->GetILGenerator();
   constructorIL->Emit( OpCodes::Ldarg_0 );
   ConstructorInfo^ superConstructor = Object::typeid->GetConstructor( gcnew array<Type^>(0) );
   constructorIL->Emit( OpCodes::Call, superConstructor );
   constructorIL->Emit( OpCodes::Ldarg_0 );
   constructorIL->Emit( OpCodes::Ldarg_1 );
   constructorIL->Emit( OpCodes::Stfld, myFieldBuilder );
   constructorIL->Emit( OpCodes::Ret );

   // Create the MyMethod method.
   MethodBuilder^ myMethodBuilder = myTypeBuilder->DefineMethod( "MyMethod", MethodAttributes::Public, String::typeid, nullptr );
   ILGenerator^ methodIL = myMethodBuilder->GetILGenerator();
   methodIL->Emit( OpCodes::Ldarg_0 );
   methodIL->Emit( OpCodes::Ldfld, myFieldBuilder );
   methodIL->Emit( OpCodes::Ret );
   Console::WriteLine( "Name               : {0}", myFieldBuilder->Name );
   Console::WriteLine( "DeclaringType      : {0}", myFieldBuilder->DeclaringType );
   Console::WriteLine( "Type               : {0}", myFieldBuilder->FieldType );
   Console::WriteLine( "Token              : {0}", myFieldBuilder->GetToken().Token );
   return myTypeBuilder->CreateType();
}

int main()
{
   try
   {
      Type^ myType = CreateType( Thread::GetDomain() );

      // Create an instance of the S"HelloWorld" class.
      array<Object^>^type = {"HelloWorld"};
      Object^ helloWorld = Activator::CreateInstance( myType, type );

      // Invoke the S"MyMethod" method of the S"MyClass" class.
      Object^ myObject = myType->InvokeMember( "MyMethod", BindingFlags::InvokeMethod, nullptr, helloWorld, nullptr );
      Console::WriteLine( "MyClass::MyMethod returned: \"{0}\"", myObject );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception Caught {0}", e->Message );
   }
}

System.Object
   System.Reflection.MemberInfo
     System.Reflection.FieldInfo
      System.Reflection.Emit.FieldBuilder

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft