FieldInfo.FieldHandle Property

Gets a RuntimeFieldHandle, which is 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.

Implements

_FieldInfo.FieldHandle

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

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

using System;
using System.Reflection;

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

public class FieldInfo_FieldHandle
{
    public static void Main()
    {

        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(myFieldHandle);
            }
            else
            {
                Console.WriteLine("The myFieldInfo object is null.");
            }
        }  
        catch(Exception e)
        {
            Console.WriteLine("Exception: {0}", e.Message);
        }
    }

    public static void DisplayFieldHandle(RuntimeFieldHandle myFieldHandle)
    {
        // Get the type from the handle.
        FieldInfo myField = FieldInfo.GetFieldFromHandle(myFieldHandle);      

        // Display the type.
        Console.WriteLine("\nDisplaying the field from the handle.\n");
        Console.WriteLine("The type is {0}.", myField.ToString());
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft