FieldInfo.GetFieldFromHandle Method (RuntimeFieldHandle, RuntimeTypeHandle)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<ComVisibleAttribute(False)> _
Public Shared Function GetFieldFromHandle ( _
    handle As RuntimeFieldHandle, _
    declaringType As RuntimeTypeHandle _
) As FieldInfo
[ComVisibleAttribute(false)]
public static FieldInfo GetFieldFromHandle(
    RuntimeFieldHandle handle,
    RuntimeTypeHandle declaringType
)

Parameters

  • handle
    Type: System.RuntimeFieldHandle
    A structure that contains the handle to the internal metadata representation of a field.
  • declaringType
    Type: System.RuntimeTypeHandle
    A structure that contains the handle to the generic type that defines the field.

Return Value

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

Exceptions

Exception Condition
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.

MethodAccessException

The member is invoked late-bound through mechanisms such as Type.InvokeMember.

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.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 This member is not supported in Silverlight for Windows Phone.

Examples

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.

Imports System.Reflection

' A generic class with a field whose type is specified by the 
' generic type parameter of the class.
Public Class Test(Of T)
   Public TestField As T
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        ' Get type handles for Test(Of String) and its field.
        Dim rth As RuntimeTypeHandle = _
            GetType(Test(Of String)).TypeHandle
        Dim rfh As RuntimeFieldHandle = _
            GetType(Test(Of String)).GetField("TestField").FieldHandle

        ' When a field belongs to a constructed generic type, 
        ' such as Test(Of 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
            Dim f1 As FieldInfo = FieldInfo.GetFieldFromHandle(rfh)
        Catch ex As Exception
            outputBlock.Text &= _
                String.Format("{0}: {1}", ex.GetType().Name, ex.Message)
        End Try

        ' To get the FieldInfo for a field on a generic type, use the
        ' overload that specifies the type handle.
        Dim fi As FieldInfo = FieldInfo.GetFieldFromHandle(rfh, rth)
        outputBlock.Text &= String.Format(vbCrLf & "The type of {0} is: {1}", _
            fi.Name, fi.FieldType)

        ' All constructions of Test(Of 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(Of Object).
        fi = FieldInfo.GetFieldFromHandle(rfh, _
                               GetType(Test(Of Object)).TypeHandle)
        outputBlock.Text &= String.Format(vbCrLf & "The type of {0} is: {1}", _
            fi.Name, fi.FieldType)

        ' Each construction of Test(Of 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, _
                               GetType(Test(Of Integer)).TypeHandle)
        Catch ex As Exception
            outputBlock.Text &= _
                String.Format(vbCrLf & "{0}: {1}", ex.GetType().Name, ex.Message)
        End Try

    End Sub
End Class

' 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.
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 Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        // 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)
        {
            outputBlock.Text += String.Format("{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);
        outputBlock.Text += String.Format("\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);
        outputBlock.Text += String.Format("\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)
        {
            outputBlock.Text += String.Format("\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.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.