C# FieldInfo GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle)
Description
FieldInfo GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)
Gets a FieldInfo for the field represented by the specified handle,
for the specified generic type.
Syntax
FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)
has the following syntax.
[ComVisibleAttribute(false)]//from w ww. j a v a2 s .com
public static FieldInfo GetFieldFromHandle(
RuntimeFieldHandle handle,
RuntimeTypeHandle declaringType
)
Parameters
FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)
has the following parameters.
handle
- A RuntimeFieldHandle structure that contains the handle to the internal metadata representation of a field.declaringType
- A RuntimeTypeHandle structure that contains the handle to the generic type that defines the field.
Returns
FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)
method returns A FieldInfo object representing the field specified by handle, in the generic
type specified by declaringType.
Example
using System;/* w ww . j a v a2 s . c o m*/
using System.Reflection;
public class Test<T>
{
public T TestField;
}
public class Example
{
public static void Main()
{
RuntimeTypeHandle rth = typeof(Test<string>).TypeHandle;
RuntimeFieldHandle rfh = typeof(Test<string>).GetField("TestField").FieldHandle;
FieldInfo f1 = FieldInfo.GetFieldFromHandle(rfh);
FieldInfo fi = FieldInfo.GetFieldFromHandle(rfh, rth);
Console.WriteLine(fi.Name);
fi = FieldInfo.GetFieldFromHandle(rfh, typeof(Test<object>).TypeHandle);
Console.WriteLine(fi.Name);
fi = FieldInfo.GetFieldFromHandle(rfh, typeof(Test<int>).TypeHandle);
}
}
The code above generates the following result.