FieldInfo.FieldHandle Property gets a RuntimeFieldHandle, which is a handle to the internal metadata representation of a field.
using System;
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);
try
{
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.");
}
}
catch(Exception e)
{
Console.WriteLine("Exception: {0}", e.Message);
}
}
public static void DisplayFieldHandle(RuntimeFieldHandle myFieldHandle)
{
FieldInfo myField = FieldInfo.GetFieldFromHandle(myFieldHandle);
Console.WriteLine("The type is {0}.", myField.ToString());
}
}
Related examples in the same category