FieldBuilder Class

FieldBuilder Class

 

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

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

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

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public sealed class FieldBuilder : FieldInfo, _FieldBuilder

NameDescription
System_CAPS_pubpropertyAttributes

Indicates the attributes of this field. This property is read-only.(Overrides FieldInfo.Attributes.)

System_CAPS_pubpropertyCustomAttributes

Gets a collection that contains this member's custom attributes.(Inherited from MemberInfo.)

System_CAPS_pubpropertyDeclaringType

Indicates a reference to the Type object for the type that declares this field. This property is read-only.(Overrides MemberInfo.DeclaringType.)

System_CAPS_pubpropertyFieldHandle

Indicates the internal metadata handle for this field. This property is read-only.(Overrides FieldInfo.FieldHandle.)

System_CAPS_pubpropertyFieldType

Indicates the Type object that represents the type of this field. This property is read-only.(Overrides FieldInfo.FieldType.)

System_CAPS_pubpropertyIsAssembly

Gets a value indicating 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.)

System_CAPS_pubpropertyIsFamily

Gets a value indicating 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.)

System_CAPS_pubpropertyIsFamilyAndAssembly

Gets a value indicating 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.)

System_CAPS_pubpropertyIsFamilyOrAssembly

Gets a value indicating 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.)

System_CAPS_pubpropertyIsInitOnly

Gets a value indicating whether the field can only be set in the body of the constructor.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsLiteral

Gets a value indicating whether the value is written at compile time and cannot be changed.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsNotSerialized

Gets a value indicating whether this field has the NotSerialized attribute.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsPinvokeImpl

Gets a value indicating whether the corresponding PinvokeImpl attribute is set in FieldAttributes.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsPrivate

Gets a value indicating whether the field is private.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsPublic

Gets a value indicating whether the field is public.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsSecurityCritical

Gets a value that indicates whether the current field is security-critical or security-safe-critical at the current trust level. (Inherited from FieldInfo.)

System_CAPS_pubpropertyIsSecuritySafeCritical

Gets a value that indicates whether the current field is security-safe-critical at the current trust level. (Inherited from FieldInfo.)

System_CAPS_pubpropertyIsSecurityTransparent

Gets a value that indicates whether the current field is transparent at the current trust level.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsSpecialName

Gets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttributes enumerator.(Inherited from FieldInfo.)

System_CAPS_pubpropertyIsStatic

Gets a value indicating whether the field is static.(Inherited from FieldInfo.)

System_CAPS_pubpropertyMemberType

Gets a MemberTypes value indicating that this member is a field.(Inherited from FieldInfo.)

System_CAPS_pubpropertyMetadataToken

Gets a value that identifies a metadata element.(Inherited from MemberInfo.)

System_CAPS_pubpropertyModule

Gets the module in which the type that contains this field is being defined.(Overrides MemberInfo.Module.)

System_CAPS_pubpropertyName

Indicates the name of this field. This property is read-only.(Overrides MemberInfo.Name.)

System_CAPS_pubpropertyReflectedType

Indicates the reference to the Type object from which this object was obtained. This property is read-only.(Overrides MemberInfo.ReflectedType.)

NameDescription
System_CAPS_pubmethodEquals(Object)

Returns a value that indicates whether this instance is equal to a specified object.(Inherited from FieldInfo.)

System_CAPS_pubmethodGetCustomAttributes(Boolean)

Returns all the custom attributes defined for this field.(Overrides MemberInfo.GetCustomAttributes(Boolean).)

System_CAPS_pubmethodGetCustomAttributes(Type, Boolean)

Returns all the custom attributes defined for this field identified by the given type.(Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)

System_CAPS_pubmethodGetCustomAttributesData()

Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.(Inherited from MemberInfo.)

System_CAPS_pubmethodGetHashCode()

Returns the hash code for this instance.(Inherited from FieldInfo.)

System_CAPS_pubmethodGetOptionalCustomModifiers()

Gets an array of types that identify the optional custom modifiers of the field.(Inherited from FieldInfo.)

System_CAPS_pubmethodGetRawConstantValue()

Returns a literal value associated with the field by a compiler. (Inherited from FieldInfo.)

System_CAPS_pubmethodGetRequiredCustomModifiers()

Gets an array of types that identify the required custom modifiers of the property.(Inherited from FieldInfo.)

System_CAPS_pubmethodGetToken()

Returns the token representing this field.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodGetValue(Object)

Retrieves the value of the field supported by the given object.(Overrides FieldInfo.GetValue(Object).)

System_CAPS_pubmethodGetValueDirect(TypedReference)

Returns the value of a field supported by a given object.(Inherited from FieldInfo.)

System_CAPS_pubmethodIsDefined(Type, Boolean)

Indicates whether an attribute having the specified type is defined on a field.(Overrides MemberInfo.IsDefined(Type, Boolean).)

System_CAPS_pubmethodSetConstant(Object)

Sets the default value of this field.

System_CAPS_pubmethodSetCustomAttribute(ConstructorInfo, Byte[])

Sets a custom attribute using a specified custom attribute blob.

System_CAPS_pubmethodSetCustomAttribute(CustomAttributeBuilder)

Sets a custom attribute using a custom attribute builder.

System_CAPS_pubmethodSetMarshal(UnmanagedMarshal)

Obsolete.Describes the native marshaling of the field.

System_CAPS_pubmethodSetOffset(Int32)

Specifies the field layout.

System_CAPS_pubmethodSetValue(Object, Object)

Sets the value of the field supported by the given object.(Inherited from FieldInfo.)

System_CAPS_pubmethodSetValue(Object, Object, BindingFlags, Binder, CultureInfo)

Sets the value of the field supported by the given object.(Overrides FieldInfo.SetValue(Object, Object, BindingFlags, Binder, CultureInfo).)

System_CAPS_pubmethodSetValueDirect(TypedReference, Object)

Sets the value of the field supported by the given object.(Inherited from FieldInfo.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldBuilder.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from FieldInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldInfo.GetType()

Gets a Type object representing the FieldInfo type.(Inherited from FieldInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.(Inherited from FieldInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldInfo.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from FieldInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_FieldInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.(Inherited from FieldInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo.GetType()

Gets a Type object representing the MemberInfo class.(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.(Inherited from MemberInfo.)

NameDescription
System_CAPS_pubmethodGetCustomAttribute(Type)

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttribute(Type, Boolean)

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttribute<T>()

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttribute<T>(Boolean)

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttributes()

Overloaded. Retrieves a collection of custom attributes that are applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttributes(Type)

Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttributes<T>(Boolean)

Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodIsDefined(Type)

Overloaded. Indicates whether custom attributes of a specified type are applied to a specified member.(Defined by CustomAttributeExtensions.)

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

System_CAPS_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 System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public class FieldBuilder_Sample
{
   private static Type CreateType(AppDomain currentDomain)
   {

      // Create an assembly.
      AssemblyName myAssemblyName = new 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 "MyClass" in the assembly.
      TypeBuilder myTypeBuilder= myModuleBuilder.DefineType("MyClass",TypeAttributes.Public);

      // Define a private String field named "MyField" in the type.
      FieldBuilder myFieldBuilder= myTypeBuilder.DefineField("MyField",
          typeof(string),FieldAttributes.Private|FieldAttributes.Static);
      // Create the constructor.
      Type[] constructorArgs = { typeof(String) };
      ConstructorBuilder constructor = myTypeBuilder.DefineConstructor(
         MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
      ILGenerator constructorIL = constructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(Object).GetConstructor(new 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,typeof(String),null);
      ILGenerator methodIL = myMethodBuilder.GetILGenerator();
      methodIL.Emit(OpCodes.Ldarg_0);
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder);
      methodIL.Emit(OpCodes.Ret);
      Console.WriteLine("Name               :"+myFieldBuilder.Name);
      Console.WriteLine("DeclaringType      :"+myFieldBuilder.DeclaringType);
      Console.WriteLine("Type               :"+myFieldBuilder.FieldType);
      Console.WriteLine("Token              :"+myFieldBuilder.GetToken().Token);
      return myTypeBuilder.CreateType();
   }

   [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
   public static void Main()
   {
      try
      {
         Type myType = CreateType(Thread.GetDomain());
         // Create an instance of the "HelloWorld" class.
         Object helloWorld = Activator.CreateInstance(myType, new Object[] { "HelloWorld" });
         // Invoke the "MyMethod" method of the "MyClass" class.
         Object myObject  = myType.InvokeMember("MyMethod",
                        BindingFlags.InvokeMethod, null, helloWorld, null);
         Console.WriteLine("MyClass.MyMethod returned: \"" + myObject + "\"");
      }
      catch( Exception e)
      {
         Console.WriteLine("Exception Caught "+e.Message);
      }
  }
}

.NET Framework
Available since 1.1
Silverlight
Available since 2.0

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

Return to top
Show:
© 2016 Microsoft