FieldInfo.GetFieldFromHandle メソッド

定義

ハンドルによって表されるフィールドの FieldInfo を取得します。

オーバーロード

名前 説明
GetFieldFromHandle(RuntimeFieldHandle)

指定したハンドルによって表されるフィールドの FieldInfo を取得します。

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

指定したジェネリック型について、指定したハンドルによって表されるフィールドの FieldInfo を取得します。

GetFieldFromHandle(RuntimeFieldHandle)

ソース:
FieldInfo.CoreCLR.cs
ソース:
FieldInfo.CoreCLR.cs
ソース:
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構造体を取得し、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)

ソース:
FieldInfo.CoreCLR.cs
ソース:
FieldInfo.CoreCLR.cs
ソース:
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 オブジェクトを取得する方法を示しています。 この例では、ジェネリック型 Test<T> (Visual Basic のTest(Of T)) を、TestField 型の T という名前の 1 つのフィールドで定義します。 この例では、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)) に属するフィールドのランタイム フィールド ハンドルである場合、declaringTypeMyType<int> のランタイム型ハンドルです。 ランタイム フィールド ハンドルがジェネリック型定義のフィールドを表す場合を除き、ジェネリック型定義のランタイム型ハンドルを使用しないでください。

場合によっては、実装に互換性があります。 たとえば、単一の実装は、ジェネリック型引数の参照型を使用して、特定のジェネリック型定義から構築されるすべての型によって共有されます。 たとえば、 MyType<string>MyType<object>MyType<ArrayList> はすべて同じ実装を共有します。 この状況では、返されるFieldInfo オブジェクトは、declaringTypeの元のソースに関係なく、指定handle型のフィールドを表します。 構築された型のジェネリック型引数が参照型である場合にのみ機能するため、この方法はお勧めしません。

ジェネリック引数が値型の場合、構築された型のランタイム型ハンドルは、同じジェネリック パラメーター位置に参照型を持つ、またはその位置に異なる値型を持つコンストラクトからのランタイム フィールド ハンドルと互換性がありません。 その場合、 FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) オーバーロードを使用する唯一の方法は、 declaringType が、 handle が属する構築された型のランタイム型ハンドルであることを確認することです。

適用対象