FieldInfo.GetFieldFromHandle 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
得到 FieldInfo 一個欄位,該欄位由一個handle表示。
多載
| 名稱 | Description |
|---|---|
| GetFieldFromHandle(RuntimeFieldHandle) |
會獲得由指定 handle 所代表欄位的 a FieldInfo 。 |
| GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) |
會獲得指定句柄所代表欄位的 a FieldInfo ,且為指定的通用型別。 |
GetFieldFromHandle(RuntimeFieldHandle)
會獲得由指定 handle 所代表欄位的 a 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結構,然後利用此方法的超載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)
會獲得指定句柄所代表欄位的 a 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 包含定義欄位的通用型別的handle的結構。
傳回
一個 FieldInfo 代表由 handle所指定的欄位的物件,該欄位在由 的通用型別中 declaringType指定。
- 屬性
例外狀況
handle 無效。
-或-
declaringType 與 handle不相容。 例如, declaringType 是通用型別定義的執行時型態控制柄,且 handle 來自建構型態。
範例
以下範例展示了如何在構造的泛類類別中擷取 FieldInfo 欄位的物件。 範例定義了通用型別 Test<T>(Visual Basic 中為 Test(Of T)),其欄位名為 TestField,型別為 T。 範例中 ,當 RuntimeFieldHandleRuntimeTypeHandleT 為 String時,且 為 ,並證明如下:
若 GetFieldFromHandle(RuntimeFieldHandle) 使用方法超載,則會拋出例外。 即使場型不屬於
T,這也成立。FieldInfo若執行時型態的 handle 與執行時欄位 handle 來自相同構造,
Test<string>則 A 即成功檢索。若執行時型代柄來自相容構造,
Test<object>則會取得相容構造上欄位的 a FieldInfo 。若執行時型態的 handle 非相容構造,則會拋出例外。 此時,會指定一個值型別。
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 。 也就是說,如果 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 的執行時型態 handle 。