FieldInfo.FieldHandle Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets a handle to the internal metadata representation of a field.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.RuntimeFieldHandleA handle to the internal metadata representation of a field.
| Exception | Condition |
|---|---|
| MethodAccessException | This member is invoked late-bound through mechanisms such as Type.InvokeMember. |
The following example retrieves field information for MyField, in MyClass, and displays the field associated with the field handle.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
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"; } }
Note: