FieldInfo.GetFieldFromHandle 方法

定义

获取句柄表示的字段的字段 FieldInfo

重载

名称 说明
GetFieldFromHandle(RuntimeFieldHandle)

获取由指定句柄表示的字段的字段 FieldInfo

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

FieldInfo获取指定泛型类型的指定句柄所表示的字段。

GetFieldFromHandle(RuntimeFieldHandle)

Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs

获取由指定句柄表示的字段的字段 FieldInfo

public:
 static System::Reflection::FieldInfo ^ GetFieldFromHandle(RuntimeFieldHandle handle);
public static System.Reflection.FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle);
static member GetFieldFromHandle : RuntimeFieldHandle -> System.Reflection.FieldInfo
Public Shared Function GetFieldFromHandle (handle As RuntimeFieldHandle) As FieldInfo

参数

handle
RuntimeFieldHandle

包含 RuntimeFieldHandle 字段内部元数据表示形式的句柄的结构。

返回

一个 FieldInfo 对象,表示由 handle.

例外

handle 无效。

示例

下面的代码示例使用Type.GetFields该方法获取类型字段的对象,获取FieldInfoRuntimeFieldHandle每个字段的结构,然后使用该方法的FieldInfo此重载从句柄中检索GetFieldFromHandle对象。

using System;
using System.Reflection;

public class FieldInfo_GetFieldFromHandle
{
    public string x;
    public char y;
    public float a;
    public int b;

    public static void Main()
    {
        // Get the type of the FieldInfo_GetFieldFromHandle class.
        Type myType = typeof(FieldInfo_GetFieldFromHandle);
        // Get the fields of the FieldInfo_GetFieldFromHandle class.
        FieldInfo [] myFieldInfoArray = myType.GetFields();
        Console.WriteLine("\nThe field information of the declared" +
            " fields x, y, a, and b is:\n");
        RuntimeFieldHandle myRuntimeFieldHandle;
        for(int i = 0; i < myFieldInfoArray.Length; i++)
        {
            // Get the RuntimeFieldHandle of myFieldInfoArray.
            myRuntimeFieldHandle = myFieldInfoArray[i].FieldHandle;
            // Call the GetFieldFromHandle method.
            FieldInfo myFieldInfo = FieldInfo.GetFieldFromHandle(myRuntimeFieldHandle);
            // Display the FieldInfo of myFieldInfo.
            Console.WriteLine("{0}", myFieldInfo);
        }
    }
}
Imports System.Reflection

Public Class FieldInfo_GetFieldFromHandle
    Public x As String
    Public y As Char
    Public a As Single
    Public b As Integer

    Public Shared Sub Main()
        ' Get the type of the FieldInfo_GetFieldFromHandle class.
        Dim myType As Type = GetType(FieldInfo_GetFieldFromHandle)
        ' Get the fields of the FieldInfo_GetFieldFromHandle class.
        Dim myFieldInfoArray As FieldInfo() = myType.GetFields()
        Console.WriteLine(ControlChars.NewLine & _
           "The field information of the declared" & _
           " fields x, y, a, and b is:" & ControlChars.NewLine)
        Dim myRuntimeFieldHandle As RuntimeFieldHandle
        Dim i As Integer
        For i = 0 To myFieldInfoArray.Length - 1
            ' Get the RuntimeFieldHandle of myFieldInfoArray.
            myRuntimeFieldHandle = myFieldInfoArray(i).FieldHandle
            ' Call the GetFieldFromHandle method. 
            Dim myFieldInfo As FieldInfo = FieldInfo.GetFieldFromHandle(myRuntimeFieldHandle)
            ' Display the FieldInfo of myFieldInfo.
            Console.WriteLine("{0}", myFieldInfo)
        Next i
    End Sub
End Class

注解

句柄仅在获取句柄的应用程序域中有效。

适用于

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs
Source:
FieldInfo.CoreCLR.cs

FieldInfo获取指定泛型类型的指定句柄所表示的字段。

public:
 static System::Reflection::FieldInfo ^ GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType);
public static System.Reflection.FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType);
[System.Runtime.InteropServices.ComVisible(false)]
public static System.Reflection.FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType);
static member GetFieldFromHandle : RuntimeFieldHandle * RuntimeTypeHandle -> System.Reflection.FieldInfo
[<System.Runtime.InteropServices.ComVisible(false)>]
static member GetFieldFromHandle : RuntimeFieldHandle * RuntimeTypeHandle -> System.Reflection.FieldInfo
Public Shared Function GetFieldFromHandle (handle As RuntimeFieldHandle, declaringType As RuntimeTypeHandle) As FieldInfo

参数

handle
RuntimeFieldHandle

包含 RuntimeFieldHandle 字段内部元数据表示形式的句柄的结构。

declaringType
RuntimeTypeHandle

包含 RuntimeTypeHandle 定义字段的泛型类型的句柄的结构。

返回

一个FieldInfo对象,表示由handledeclaringType指定的泛型类型中的字段。

属性

例外

handle 无效。

-或-

declaringTypehandle 不兼容。 例如, declaringType 是泛型类型定义的运行时类型句柄,并且 handle 来自构造的类型。

示例

以下示例演示如何检索 FieldInfo 构造泛型类上字段的对象。 该示例定义泛型类型Test<T> (Visual Basic 中的 Test(Of T)),其类型为 TestField 的单个字段名为 T。 该示例获取RuntimeFieldHandle和演示以下情况RuntimeTypeHandleTString

  • 如果使用 GetFieldFromHandle(RuntimeFieldHandle) 方法重载,则会引发异常。 即使字段不是类型 T,也是如此。

  • 如果运行时类型句柄与运行时字段句柄的构造相同,则成功检索 A FieldInfo ,在本例 Test<string>中。

  • 如果运行时类型句柄来自兼容的构造,在这种情况下 Test<object>FieldInfo 将检索兼容构造上的字段。

  • 如果运行时类型句柄不是来自兼容的构造,则会引发异常。 在这种情况下,为 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.
 */
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 Main()

        ' 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
            Console.WriteLine("{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)
        Console.WriteLine(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)
        Console.WriteLine(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
            Console.WriteLine(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.

注解

句柄仅在获取句柄的应用程序域中有效。

建议的做法应 declaringType 始终是属于的构造类型的运行时类型 handle 句柄。 也就是说,如果 handle 是属于 MyType<int>(Visual Basic 中的 MyType(Of Integer))的字段的运行时字段句柄,declaringTypeMyType<int> 的运行时类型句柄。 请勿使用泛型类型定义的运行时类型句柄,除非运行时字段句柄表示泛型类型定义的字段。

在某些情况下,实现是兼容的。 例如,单个实现由使用泛型类型参数的引用类型从特定泛型类型定义构造的所有类型共享。 例如,MyType<string>MyType<object>MyType<ArrayList>所有共享相同的实现。 在这种情况下, FieldInfo 返回的对象表示指定类型的 declaringType 字段,而不考虑原始源 handle。 不建议这样做,因为它仅在构造类型的泛型类型参数是引用类型时才有效。

如果泛型参数是值类型,则构造类型的运行时类型句柄与具有相同泛型参数位置的引用类型的构造或在该位置具有不同值类型的构造的运行时字段句柄不兼容。 在这种情况下,使用 FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) 重载的唯一方法是确保 declaringType 属于该重载的运行时 handle 类型句柄。

适用于