FieldInfo.FieldHandle Property

July 28, 2014

Gets a handle to the internal metadata representation of a field.

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

public abstract RuntimeFieldHandle FieldHandle { get; }

Property Value

Type: System.RuntimeFieldHandle
A handle to the internal metadata representation of a field.

ExceptionCondition
MethodAccessException

This member is invoked late-bound through mechanisms such as Type.InvokeMember.

The handles are valid only in the application domain in which they were obtained.

The following example retrieves field information for MyField, in MyClass, and displays the field associated with the field handle.


using System;
using System.Reflection;

public class MyClass
{
   public string MyField = "Microsoft";
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      MyClass myClass = new MyClass();

      // Get the type of MyClass.
      Type myType = typeof(MyClass);

      try
      {
         // Get the field information of MyField.
         FieldInfo myFieldInfo = myType.GetField("MyField", BindingFlags.Public
             | BindingFlags.Instance);

         // Determine whether or not the FieldInfo object is null.
         if (myFieldInfo != null)
         {
            // Get the handle for the field.
            RuntimeFieldHandle myFieldHandle = myFieldInfo.FieldHandle;

            DisplayFieldHandle(outputBlock, myFieldHandle);
         }
         else
         {
            outputBlock.Text += "The myFieldInfo object is null." + "\n";
         }
      }
      catch (Exception e)
      {
         outputBlock.Text += String.Format("Exception: {0}", e.Message) + "\n";
      }
   }

   public static void DisplayFieldHandle(System.Windows.Controls.TextBlock outputBlock, RuntimeFieldHandle myFieldHandle)
   {
      // Get the type from the handle.
      FieldInfo myField = FieldInfo.GetFieldFromHandle(myFieldHandle);

      // Display the type.
      outputBlock.Text += "\nDisplaying the field from the handle.\n" + "\n";
      outputBlock.Text += String.Format("The type is {0}.", myField.ToString()) + "\n";
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft