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)]
public sealed class FieldBuilder : FieldInfo

The FieldBuilder type exposes the following members.

  NameDescription
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.)
Top

  NameDescription
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.)
Top

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

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

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "DynamicAssembly";
      AssemblyBuilder myAssembly =
         AppDomain.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);
      outputBlock.Text += "Name: " + myFieldBuilder.Name + "\n";
      outputBlock.Text += "DeclaringType: " + myFieldBuilder.DeclaringType + "\n";
      outputBlock.Text += "Type: " + myFieldBuilder.FieldType + "\n";
      outputBlock.Text += "Token: " + myFieldBuilder.GetToken().Token + "\n";

      Type myType = myTypeBuilder.CreateType();
      // 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);
      outputBlock.Text += "MyClass.MyMethod returned: \"" + myObject + "\"" + "\n";
   }
}


Silverlight

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

ADD
Show:
© 2014 Microsoft