FieldInfo.GetFieldFromHandle 메서드

정의

핸들이 나타내는 필드의 FieldInfo를 가져옵니다.

오버로드

GetFieldFromHandle(RuntimeFieldHandle)

지정된 핸들이 나타내는 필드의 FieldInfo를 가져옵니다.

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

지정된 제네릭 형식에 대해 지정된 핸들이 나타내는 필드의 FieldInfo를 가져옵니다.

GetFieldFromHandle(RuntimeFieldHandle)

지정된 핸들이 나타내는 필드의 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로 지정된 필드를 나타내는 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를 가져옵니다.

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로 지정된 필드를 나타내는 FieldInfo 개체입니다.

특성

예외

handle이 잘못되었습니다.

또는 declaringTypehandle과 호환되지 않는 경우. 예를 들어 declaringType은 제네렉 형식 정의의 런타임 형식 핸들이며 handle은 생성된 형식에서 만들어집니다.

예제

다음 예제에서는 생성된 제네릭 클래스의 필드에 대한 개체를 검색 FieldInfo 하는 방법을 보여 줍니다. 이 예제에서는 이름이 Test<T>인 단일 필드TestField가 있는 제네릭 형식(Test(Of T)Visual Basic)을 정의합니다T. 이 예제에서는 다음과 같은 경우 T String를 가져오 RuntimeFieldHandle RuntimeTypeHandle 고 다음을 보여 줍니다.

  • 메서드 오버로드를 GetFieldFromHandle(RuntimeFieldHandle) 사용하는 경우 예외가 throw됩니다. 필드가 형식 T이 아닌 경우에도 마찬가지입니다.

  • 이 경우 Test<string>런타임 형식 핸들이 런타임 필드 핸들과 동일한 생성에서 가져온 경우 A FieldInfo 가 성공적으로 검색됩니다.

  • 런타임 형식 핸들이 호환되는 생성에서 가져온 경우 이 경우 Test<object>FieldInfo 호환되는 생성의 필드가 검색됩니다.

  • 런타임 형식 핸들이 호환되는 생성에서 생성되지 않은 경우 예외가 throw됩니다. 이 경우 값 형식은 에 대해 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) declaringType 에 속하는 MyType<int> 필드에 대한 런타임 필드 핸들인 경우 handle 런타임 형식 핸들MyType<int>입니다. 런타임 필드 핸들이 제네릭 형식 정의의 필드를 나타내지 않는 한 제네릭 형식 정의의 런타임 형식 핸들을 사용하지 마세요.

구현은 경우에 따라 호환됩니다. 예를 들어 단일 구현은 제네릭 형식 인수에 대한 참조 형식을 사용하여 특정 제네릭 형식 정의에서 생성된 모든 형식에서 공유됩니다. 예를 들어MyType<string>, 모두 MyType<object>``MyType<ArrayList> 동일한 구현을 공유합니다. 이 경우 FieldInfo 반환되는 개체는 원래 원본에 관계없이 지정하는 declaringType 형식의 handle필드를 나타냅니다. 생성된 형식의 제네릭 형식 인수가 참조 형식인 경우에만 작동하므로 이 방법은 권장되지 않습니다.

제네릭 인수가 값 형식인 경우 생성된 형식의 런타임 형식 핸들은 참조 형식이 동일한 제네릭 매개 변수 위치에 있거나 해당 위치에 다른 값 형식이 있는 생성의 런타임 필드 핸들과 호환되지 않습니다. 이 경우 오버로드를 사용하는 FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) 유일한 방법은 속한 생성된 형식 handle 에 대한 런타임 형식 핸들인지 확인하는 declaringType 것입니다.

적용 대상