C# FieldInfo FieldHandle

Description

FieldInfo FieldHandle Gets a RuntimeFieldHandle, which is a handle to the internal metadata representation of a field.

Syntax

FieldInfo.FieldHandle has the following syntax.


public abstract RuntimeFieldHandle FieldHandle { get; }

Example

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


using System;/* w  ww.  j a v a2  s  .  c om*/
using System.Reflection;

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

public class FieldInfo_FieldHandle
{
    public static void Main()
    {
        MyClass myClass =new MyClass();
        Type myType = typeof(MyClass);

        FieldInfo myFieldInfo = myType.GetField("MyField", BindingFlags.Public | BindingFlags.Instance);

        if(myFieldInfo!=null){
            RuntimeFieldHandle myFieldHandle=myFieldInfo.FieldHandle;

            DisplayFieldHandle(myFieldHandle);
        }else{
            Console.WriteLine("The myFieldInfo object is null.");
        }
    }

    public static void DisplayFieldHandle(RuntimeFieldHandle myFieldHandle)
    {
        FieldInfo myField = FieldInfo.GetFieldFromHandle(myFieldHandle);      
        Console.WriteLine("The type is {0}.", myField.ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo