FieldBuilder Class

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

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

<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class FieldBuilder _
	Inherits FieldInfo

The FieldBuilder type exposes the following members.

Public propertyAttributesGets the attributes of this field. (Overrides FieldInfo.Attributes.)
Public propertyDeclaringTypeGets a reference to the Type object for the type that declares this field. (Overrides MemberInfo.DeclaringType.)
Public propertyFieldHandleGets the internal metadata handle for this field. (Overrides FieldInfo.FieldHandle.)
Public propertyFieldTypeGets the Type object that represents the type of this field. (Overrides FieldInfo.FieldType.)
Public propertyIsAssemblyGets a value that indicates whether the potential visibility of this field is described by FieldAttributes.Assembly; that is, the field is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly. (Inherited from FieldInfo.)
Public propertyIsFamilyGets a value that indicates whether the visibility of this field is described by FieldAttributes.Family; that is, the field is visible only within its class and derived classes. (Inherited from FieldInfo.)
Public propertyIsFamilyAndAssemblyGets a value that indicates whether the visibility of this field is described by FieldAttributes.FamANDAssem; that is, the field can be accessed from derived classes, but only if they are in the same assembly. (Inherited from FieldInfo.)
Public propertyIsFamilyOrAssemblyGets a value that indicates whether the potential visibility of this field is described by FieldAttributes.FamORAssem; that is, the field can be accessed by derived classes wherever they are, and by classes in the same assembly. (Inherited from FieldInfo.)
Public propertyIsInitOnlyGets a value that indicates whether the field can be set only in the body of the constructor. (Inherited from FieldInfo.)
Public propertyIsLiteralGets a value that indicates whether the value is written at compile time and cannot be changed. (Inherited from FieldInfo.)
Public propertyIsNotSerializedGets a value that indicates whether this field has the NotSerialized attribute. (Inherited from FieldInfo.)
Public propertyIsPinvokeImplGets a value that indicates whether the corresponding PinvokeImpl attribute is set in FieldAttributes. (Inherited from FieldInfo.)
Public propertyIsPrivateGets a value that indicates whether the field is private. (Inherited from FieldInfo.)
Public propertyIsPublicGets a value that indicates whether the field is public. (Inherited from FieldInfo.)
Public propertyIsSpecialNameGets a value that indicates whether the field has a name that has special significance. (Inherited from FieldInfo.)
Public propertyIsStaticGets a value that indicates whether the field is static (Shared in Visual Basic). (Inherited from FieldInfo.)
Public propertyMemberTypeGets a value that indicates that this member is a field. (Inherited from FieldInfo.)
Public propertyMetadataTokenGets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public propertyModuleGets the module in which the type that contains this field is being defined. (Overrides MemberInfo.Module.)
Public propertyNameGets the name of this field. (Overrides MemberInfo.Name.)
Public propertyReflectedTypeGets the reference to the Type object from which this object was obtained. (Overrides MemberInfo.ReflectedType.)

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetCustomAttributes(Boolean)Returns all the custom attributes defined for this field. (Overrides MemberInfo.GetCustomAttributes(Boolean).)
Public methodGetCustomAttributes(Type, Boolean)Returns all the custom attributes defined for this field identified by the given type. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetRawConstantValueReturns a literal value associated with the field by a compiler. (Inherited from FieldInfo.)
Public methodGetTokenReturns the token representing this field.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueRetrieves the value of the field supported by the given object. (Overrides FieldInfo.GetValue(Object).)
Public methodIsDefinedGets a value that indicates whether an attribute having the specified type is defined on a field. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetConstantSets the default value of this field.
Public methodSetCustomAttributeSets a custom attribute using a custom attribute builder.
Public methodSetValue(Object, Object)Sets the value of the field that is supported by the given object. (Inherited from FieldInfo.)
Public methodSetValue(Object, Object, BindingFlags, Binder, CultureInfo)Sets the value of the field supported by the given object. (Overrides FieldInfo.SetValue(Object, Object, BindingFlags, Binder, CultureInfo).)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Get an instance of FieldBuilder by calling the TypeBuilder.DefineField method.


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.Reflection
Imports System.Reflection.Emit

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create an assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "DynamicAssembly"
      Dim myAssembly As AssemblyBuilder = _
         AppDomain.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)
      outputBlock.Text &= "Name: " & myFieldBuilder.Name & vbCrLf
      outputBlock.Text &= "DeclaringType: " & myFieldBuilder.DeclaringType.ToString() & vbCrLf
      outputBlock.Text &= "Type: " & myFieldBuilder.FieldType.ToString() & vbCrLf
      outputBlock.Text &= "Token: " & myFieldBuilder.GetToken().Token.ToString() & vbCrLf

      Dim myType As Type = myTypeBuilder.CreateType()
      ' 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)
      outputBlock.Text &= "MyClass.MyMethod returned: """ & myObject & """" & vbCrLf

   End Sub 
End Class 


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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

Community Additions