Export (0) Print
Expand All

FieldInfo.FieldHandle Property

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";
   }
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

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

Community Additions

ADD
Show:
© 2015 Microsoft