This documentation is archived and is not being maintained.

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 NotInheritable Class FieldBuilder
	Inherits FieldInfo
	Implements _FieldBuilder
Dim instance As FieldBuilder

/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class FieldBuilder extends FieldInfo implements _FieldBuilder
public final class FieldBuilder extends FieldInfo implements _FieldBuilder


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.


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.

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Class FieldBuilder_Sample
   Private Shared Function CreateType(currentDomain As AppDomain) As Type

      ' Create an assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "DynamicAssembly"
      Dim myAssembly As AssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
      ' Create a dynamic module in Dynamic Assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule")
      ' Define a public class named "MyClass" in the assembly.
      Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyClass", _
      ' Define a private String field named "MyField" in the type.
      Dim myFieldBuilder As FieldBuilder = myTypeBuilder.DefineField("MyField", _
                  GetType(String), FieldAttributes.Private Or FieldAttributes.Static)
      ' Create the constructor.
      Dim constructorArgs As Type() = {GetType(String)}
      Dim constructor As ConstructorBuilder = _
                  myTypeBuilder.DefineConstructor(MethodAttributes.Public, _
                           CallingConventions.Standard, constructorArgs)
      Dim constructorIL As ILGenerator = constructor.GetILGenerator()
      Dim superConstructor As ConstructorInfo = GetType(Object).GetConstructor(New Type() {})
      constructorIL.Emit(OpCodes.Call, superConstructor)
      constructorIL.Emit(OpCodes.Stfld, myFieldBuilder)

      ' Create the MyMethod method.
      Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod",  _
                        MethodAttributes.Public, GetType(String), Nothing)
      Dim methodIL As ILGenerator = myMethodBuilder.GetILGenerator()
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder)
      Console.WriteLine("Name               :" + myFieldBuilder.Name)
      Console.WriteLine("DeclaringType      :" + myFieldBuilder.DeclaringType.ToString())
      Console.WriteLine("Type               :" + myFieldBuilder.FieldType.ToString())
      Console.WriteLine("Token              :" + myFieldBuilder.GetToken().Token.ToString())
      Return myTypeBuilder.CreateType()
   End Function 'CreateType

   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
         Dim myType As Type = CreateType(Thread.GetDomain())
         ' Create an instance of the "HelloWorld" class.
         Dim helloWorld As Object = Activator.CreateInstance(myType, New Object() {"HelloWorld"})
         ' Invoke the "MyMethod" method of the "MyClass" class.
         Dim myObject As Object = myType.InvokeMember("MyMethod", _
                     BindingFlags.InvokeMethod, Nothing, helloWorld, Nothing)
         Console.WriteLine("MyClass.MyMethod returned: " & Microsoft.VisualBasic.Chr(34) & myObject  & Microsoft.VisualBasic.Chr(34) )
     Catch e as Exception
            Console.WriteLine("Exception Caught "+e.Message)
     End Try
   End Sub 'Main
End Class 'FieldBuilder_Sample


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