共用方式為


FieldInfo.GetFieldFromHandle 方法

定義

取得控制代碼所表示之欄位的 FieldInfo

多載

GetFieldFromHandle(RuntimeFieldHandle)

取得指定的控制代碼所表示之欄位的 FieldInfo

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

針對指定的泛型型別,取得指定的控制代碼所表示之欄位的 FieldInfo

GetFieldFromHandle(RuntimeFieldHandle)

來源:
FieldInfo.CoreCLR.cs
來源:
FieldInfo.CoreCLR.cs
來源:
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使用 方法來取得FieldInfo型別欄位的物件、取得RuntimeFieldHandle每個欄位的結構,然後使用方法的GetFieldFromHandle這個多載從句柄擷取FieldInfo物件。

using namespace System;
using namespace System::Reflection;

public ref class FieldInfo_GetFieldFromHandle
{
public:
   String^ x;
   Char y;
   float a;
   int b;
};

int main()
{
   // Get the type of the FieldInfo_GetFieldFromHandle class.
   Type^ myType = FieldInfo_GetFieldFromHandle::typeid;

   // Get the fields of the FieldInfo_GetFieldFromHandle class.
   array<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 );
   }
}
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)

來源:
FieldInfo.CoreCLR.cs
來源:
FieldInfo.CoreCLR.cs
來源:
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 物件,在 handle 所指定的泛型型別中,表示 declaringType 指定的欄位。

屬性

例外狀況

handle 無效。

-或-

declaringTypehandle 不相容。 例如,declaringType 是泛型型別定義的執行階段型別控制代碼,且 handle 來自建構的型別。

範例

下列範例示範如何擷取 FieldInfo 建構泛型類別上字段的物件。 此範例會在 Visual Basic) 中定義泛型型Test<T>別 (Test(Of T),且類型為 的單TestFieldT一字段。 此範例會取得 RuntimeFieldHandleRuntimeTypeHandle 的 和 ,其中 是 TString,並示範下列各項:

  • 如果使用方法多載, GetFieldFromHandle(RuntimeFieldHandle) 則會擲回例外狀況。 即使欄位不是 類型 T,也是如此。

  • 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) 之欄位的運行時間欄位句柄, declaringType 則為的 MyType<int>運行時間類型句柄。 除非運行時間欄位句柄代表泛型類型定義的欄位,否則請勿使用泛型型別定義的運行時間類型句柄。

在某些情況下,實作相容。 例如,單一實作是由所有從特定泛型類型定義建構的型別共用,方法是使用泛型類型自變數的參考型別。 例如, MyType<string>MyType<object>MyType<ArrayList> 全都會共用相同的實作。 在此情況下, FieldInfo 傳回的物件代表類型上的 declaringType 字段,不論的原始來源 handle為何。 不建議使用這個做法,因為只有在建構型別的泛型型別自變數是參考型別時才適用。

如果泛型自變數是實值型別,則建構型別的運行時間類型句柄與具有相同泛型參數位置之參考類型的建構與運行時間字段句柄不相容,或在該位置中具有不同實值型別的建構。 在此情況下,使用 FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) 多載的唯一方法是確保 declaringType 是屬於 的建構型別的運行時間類型 handle 句柄。

適用於