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 構造体。

戻り値

handle によって指定されたフィールドを表す FieldInfo オブジェクト。

例外

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 構造体。

戻り値

handle によって指定されたジェネリック型で、declaringType によって指定されたフィールドを表す FieldInfo オブジェクト。

属性

例外

handle が無効です。

または

declaringTypehandle と互換性がありません。 たとえば、declaringType はジェネリック型定義の実行時型ハンドルであり、handle は構築された型に基づいています。

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

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

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

適用対象