FieldInfo.GetFieldFromHandle Method (RuntimeFieldHandle, RuntimeTypeHandle)

Gets a FieldInfo for the field represented by the specified handle, for the specified generic type.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(false)]
public static FieldInfo GetFieldFromHandle(
	RuntimeFieldHandle handle,
	RuntimeTypeHandle declaringType
)

Parameters

handle
Type: System.RuntimeFieldHandle

A RuntimeFieldHandle structure that contains the handle to the internal metadata representation of a field.

declaringType
Type: System.RuntimeTypeHandle

A RuntimeTypeHandle structure that contains the handle to the generic type that defines the field.

Return Value

Type: System.Reflection.FieldInfo
A FieldInfo object representing the field specified by handle, in the generic type specified by declaringType.

ExceptionCondition
ArgumentException

handle is invalid.

-or-

declaringType is not compatible with handle. For example, declaringType is the runtime type handle of the generic type definition, and handle comes from a constructed type. See Remarks.

Handles are valid only in the application domain in which they were obtained.

The recommended practice is that declaringType should always be the runtime type handle of the constructed type that handle belongs to. That is, if handle is a runtime field handle for a field that belongs to MyType<int> (MyType(Of Integer) in Visual Basic), declaringType is the runtime type handle for MyType<int>. Do not use the runtime type handle of the generic type definition, unless the runtime field handle represents a field on the generic type definition.

Implementations are compatible in some cases. For example, a single implementation is shared by all types that are constructed from a particular generic type definition by using reference types for the generic type arguments. For example, MyType<string>, MyType<object>, and MyType<ArrayList> all share the same implementation. In this situation, the FieldInfo object that is returned represents a field on the type that declaringType specifies, regardless of the original source of handle. This practice is not recommended, because it works only if the generic type arguments of the constructed type are reference types.

If a generic argument is a value type, the runtime type handle of the constructed type is not compatible with runtime field handles from constructions that have a reference type in the same generic parameter position, or that have a different value type in that position. In that case, the only way to use the FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) overload is to ensure that declaringType is the runtime type handle for the constructed type that handle belongs to.

The following example shows how to retrieve FieldInfo objects for fields on constructed generic classes. The example defines the generic type Test<T> (Test(Of T) in Visual Basic) with a single field named TestField, of type T. The example gets the RuntimeFieldHandle and RuntimeTypeHandle for the case where T is String, and demonstrates the following:

  • An exception is thrown if the GetFieldFromHandle(RuntimeFieldHandle) method overload is used. This is true even if the field is not of type T.

  • A FieldInfo is retrieved successfully if the runtime type handle is from the same construction as the runtime field handle, in this case Test<string>.

  • If the runtime type handle is from a compatible construction, in this case Test<object>, a FieldInfo for the field on the compatible construction is retrieved.

  • If the runtime type handle is not from a compatible construction, an exception is thrown. In this case, a value type is specified for T.

using System;
using System.Reflection;

// A generic class with a field whose type is specified by the  
// generic type parameter of the class. 
public class Test<T>
{
    public T TestField;
}

public class Example
{
    public static void Main()
    {
        // Get type handles for Test<String> and its field.
        RuntimeTypeHandle rth = typeof(Test<string>).TypeHandle;
        RuntimeFieldHandle rfh = typeof(Test<string>).GetField("TestField").FieldHandle;

        // When a field belongs to a constructed generic type,  
        // such as Test<String>, retrieving the field from the 
        // field handle requires the type handle of the constructed 
        // generic type. An exception is thrown if the type is not 
        // included. 
        try
        {
            FieldInfo f1 = FieldInfo.GetFieldFromHandle(rfh);
        }
        catch(Exception ex)
        {
            Console.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message);
        }

        // To get the FieldInfo for a field on a generic type, use the 
        // overload that specifies the type handle.
        FieldInfo fi = FieldInfo.GetFieldFromHandle(rfh, rth);
        Console.WriteLine("\r\nThe type of {0} is: {1}", fi.Name, fi.FieldType);

        // All constructions of Test<T> for which T is a reference 
        // type share the same implementation, so the same runtime  
        // field handle can be used to retrieve the FieldInfo for  
        // TestField on any such construction. Here the runtime field 
        // handle is used with Test<Object>.
        fi = FieldInfo.GetFieldFromHandle(rfh, typeof(Test<object>).TypeHandle);
        Console.WriteLine("\r\nThe type of {0} is: {1}", fi.Name, fi.FieldType);

        // Each construction of Test<T> for which T is a value type 
        // has its own unique implementation, and an exception is thrown 
        // if you supply a constructed type other than the one that  
        // the runtime field handle belongs to.   
        try
        {
            fi = FieldInfo.GetFieldFromHandle(rfh, typeof(Test<int>).TypeHandle);
        }
        catch(Exception ex)
        {
            Console.WriteLine("\r\n{0}: {1}", ex.GetType().Name, ex.Message);
        }
    }
}

/* This code example produces output similar to the following:

ArgumentException: Cannot resolve field TestField because the declaring type of
the field handle Test`1[T] is generic. Explicitly provide the declaring type to
GetFieldFromHandle.

The type of TestField is: System.String

The type of TestField is: System.Object

ArgumentException: Type handle 'Test`1[System.Int32]' and field handle with decl
aring type 'Test`1[System.__Canon]' are incompatible. Get RuntimeFieldHandle and
 declaring RuntimeTypeHandle off the same FieldInfo.
 */

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft