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)

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

The FieldBuilder type exposes the following members.

  NameDescription
Public propertyAttributesIndicates the attributes of this field. This property is read-only. (Overrides FieldInfo.Attributes.)
Public propertyCustomAttributesGets a collection that contains this member's custom attributes. (Inherited from MemberInfo.)
Public propertyDeclaringTypeIndicates a reference to the Type object for the type that declares this field. This property is read-only. (Overrides MemberInfo.DeclaringType.)
Public propertyFieldHandleIndicates the internal metadata handle for this field. This property is read-only. (Overrides FieldInfo.FieldHandle.)
Public propertyFieldTypeIndicates the Type object that represents the type of this field. This property is read-only. (Overrides FieldInfo.FieldType.)
Public propertyIsAssemblyGets 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.)
Public propertyIsFamilyGets 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.)
Public propertyIsFamilyAndAssemblyGets 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.)
Public propertyIsFamilyOrAssemblyGets 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.)
Public propertyIsInitOnlyGets a value indicating whether the field can only be set in the body of the constructor. (Inherited from FieldInfo.)
Public propertyIsLiteralGets a value indicating whether the value is written at compile time and cannot be changed. (Inherited from FieldInfo.)
Public propertyIsNotSerializedGets a value indicating whether this field has the NotSerialized attribute. (Inherited from FieldInfo.)
Public propertyIsPinvokeImplGets a value indicating whether the corresponding PinvokeImpl attribute is set in FieldAttributes. (Inherited from FieldInfo.)
Public propertyIsPrivateGets a value indicating whether the field is private. (Inherited from FieldInfo.)
Public propertyIsPublicGets a value indicating whether the field is public. (Inherited from FieldInfo.)
Public propertyIsSecurityCriticalGets a value that indicates whether the current field is security-critical or security-safe-critical at the current trust level. (Inherited from FieldInfo.)
Public propertyIsSecuritySafeCriticalGets a value that indicates whether the current field is security-safe-critical at the current trust level. (Inherited from FieldInfo.)
Public propertyIsSecurityTransparentGets a value that indicates whether the current field is transparent at the current trust level. (Inherited from FieldInfo.)
Public propertyIsSpecialNameGets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttributes enumerator. (Inherited from FieldInfo.)
Public propertyIsStaticGets a value indicating whether the field is static. (Inherited from FieldInfo.)
Public propertyMemberTypeGets a MemberTypes value indicating 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 propertyNameIndicates the name of this field. This property is read-only. (Overrides MemberInfo.Name.)
Public propertyReflectedTypeIndicates the reference to the Type object from which this object was obtained. This property is read-only. (Overrides MemberInfo.ReflectedType.)
Top

  NameDescription
Public methodEqualsReturns a value that indicates whether this instance is equal to a specified object. (Inherited from FieldInfo.)
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 methodGetCustomAttributesDataReturns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member. (Inherited from MemberInfo.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from FieldInfo.)
Public methodGetOptionalCustomModifiersGets an array of types that identify the optional custom modifiers of the field. (Inherited from FieldInfo.)
Public methodGetRawConstantValueReturns a literal value associated with the field by a compiler. (Inherited from FieldInfo.)
Public methodGetRequiredCustomModifiersGets an array of types that identify the required custom modifiers of the property. (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 methodGetValueDirectReturns the value of a field supported by a given object. (Inherited from FieldInfo.)
Public methodIsDefinedIndicates whether an attribute having the specified type is defined on a field. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Public methodSetConstantSets the default value of this field.
Public methodSetCustomAttribute(CustomAttributeBuilder)Sets a custom attribute using a custom attribute builder.
Public methodSetCustomAttribute(ConstructorInfo, Byte[])Sets a custom attribute using a specified custom attribute blob.
Public methodSetMarshal Obsolete. Describes the native marshaling of the field.
Public methodSetOffsetSpecifies the field layout.
Public methodSetValue(Object, Object)Sets the value of the field 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 methodSetValueDirectSets the value of the field supported by the given object. (Inherited from FieldInfo.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodGetCustomAttribute(Type)Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttribute(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.)
Public Extension MethodGetCustomAttribute<T>()Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttribute<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.)
Public Extension MethodGetCustomAttributes()Overloaded. Retrieves a collection of custom attributes that are applied to a specified member. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes(Type)Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes<T>()Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes<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.)
Public Extension MethodIsDefinedIndicates whether custom attributes of a specified type are applied to a specified member. (Defined by CustomAttributeExtensions.)
Top

  NameDescription
Explicit interface implemetationPrivate method_FieldBuilder.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers.
Explicit interface implemetationPrivate method_FieldBuilder.GetTypeInfoRetrieves the type information for an object, which can then be used to get the type information for an interface.
Explicit interface implemetationPrivate method_FieldBuilder.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1).
Explicit interface implemetationPrivate method_FieldBuilder.InvokeProvides access to properties and methods exposed by an object.
Explicit interface implemetationPrivate method_FieldInfo.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from FieldInfo.)
Explicit interface implemetationPrivate method_FieldInfo.GetTypeGets a Type object representing the FieldInfo type. (Inherited from FieldInfo.)
Explicit interface implemetationPrivate method_FieldInfo.GetTypeInfoRetrieves the type information for an object, which can then be used to get the type information for an interface. (Inherited from FieldInfo.)
Explicit interface implemetationPrivate method_FieldInfo.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from FieldInfo.)
Explicit interface implemetationPrivate method_FieldInfo.InvokeProvides access to properties and methods exposed by an object. (Inherited from FieldInfo.)
Explicit interface implemetationPrivate method_MemberInfo.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from MemberInfo.)
Explicit interface implemetationPrivate method_MemberInfo.GetTypeGets a Type object representing the MemberInfo class. (Inherited from MemberInfo.)
Explicit interface implemetationPrivate method_MemberInfo.GetTypeInfoRetrieves the type information for an object, which can then be used to get the type information for an interface. (Inherited from MemberInfo.)
Explicit interface implemetationPrivate method_MemberInfo.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from MemberInfo.)
Explicit interface implemetationPrivate method_MemberInfo.InvokeProvides access to properties and methods exposed by an object. (Inherited from MemberInfo.)
Top

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.

NoteNote

The HostProtectionAttribute attribute applied to this type or member 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.

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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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