FieldInfo.GetFieldFromHandle FieldInfo.GetFieldFromHandle FieldInfo.GetFieldFromHandle FieldInfo.GetFieldFromHandle Method

정의

핸들이 나타내는 필드의 FieldInfo를 가져옵니다.Gets a FieldInfo for the field represented by a handle.

오버로드

GetFieldFromHandle(RuntimeFieldHandle) GetFieldFromHandle(RuntimeFieldHandle) GetFieldFromHandle(RuntimeFieldHandle) GetFieldFromHandle(RuntimeFieldHandle)

지정된 핸들이 나타내는 필드의 FieldInfo를 가져옵니다.Gets a FieldInfo for the field represented by the specified handle.

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

지정된 제네릭 형식에 대해 지정된 핸들이 나타내는 필드의 FieldInfo를 가져옵니다.Gets a FieldInfo for the field represented by the specified handle, for the specified generic type.

GetFieldFromHandle(RuntimeFieldHandle) GetFieldFromHandle(RuntimeFieldHandle) GetFieldFromHandle(RuntimeFieldHandle) GetFieldFromHandle(RuntimeFieldHandle)

지정된 핸들이 나타내는 필드의 FieldInfo를 가져옵니다.Gets a FieldInfo for the field represented by the specified handle.

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 RuntimeFieldHandle RuntimeFieldHandle

필드의 내부 메타데이터 표현에 대한 핸들이 들어 있는 RuntimeFieldHandle 구조체입니다.A RuntimeFieldHandle structure that contains the handle to the internal metadata representation of a field.

반환

handle로 지정된 필드를 나타내는 FieldInfo 개체입니다.A FieldInfo object representing the field specified by handle.

예외

handle이 잘못되었습니다.handle is invalid.

예제

다음 코드 예제에서는 합니다 Type.GetFields 메서드를 FieldInfo 형식의 필드에 대 한 개체를 가져옵니다를 RuntimeFieldHandle 한 다음 각 필드에 검색에 대 한 구조는 FieldInfo 합니다 가오버이로드를사용하여핸들에서개체GetFieldFromHandle메서드.The following code example uses the Type.GetFields method to get FieldInfo objects for the fields of a type, gets a RuntimeFieldHandle structure for each field, and then retrieves the FieldInfo objects from the handles using this overload of the GetFieldFromHandle method.

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

설명

핸들은는 가져온 애플리케이션 도메인 에서만 유효 합니다.Handles are valid only in the application domain in which they were obtained.

보안

ReflectionPermission
호출 될 때 런타임에 바인딩된 메커니즘을 통해 같은 InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])합니다.when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 연관된 열거형: MemberAccessAssociated enumeration: MemberAccess.

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

지정된 제네릭 형식에 대해 지정된 핸들이 나타내는 필드의 FieldInfo를 가져옵니다.Gets a FieldInfo for the field represented by the specified handle, for the specified generic type.

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
Public Shared Function GetFieldFromHandle (handle As RuntimeFieldHandle, declaringType As RuntimeTypeHandle) As FieldInfo

매개 변수

handle
RuntimeFieldHandle RuntimeFieldHandle RuntimeFieldHandle RuntimeFieldHandle

필드의 내부 메타데이터 표현에 대한 핸들이 들어 있는 RuntimeFieldHandle 구조체입니다.A RuntimeFieldHandle structure that contains the handle to the internal metadata representation of a field.

declaringType
RuntimeTypeHandle RuntimeTypeHandle RuntimeTypeHandle RuntimeTypeHandle

필드를 정의하는 제네릭 형식에 대한 핸들이 들어 있는 RuntimeTypeHandle 구조체입니다.A RuntimeTypeHandle structure that contains the handle to the generic type that defines the field.

반환

handle으로 지정된 제네릭 형식에서 declaringType로 지정된 필드를 나타내는 FieldInfo 개체입니다.A FieldInfo object representing the field specified by handle, in the generic type specified by declaringType.

예외

handle이 잘못되었습니다.handle is invalid.

또는-or- declaringTypehandle과 호환되지 않는 경우.declaringType is not compatible with handle. 예를 들어 declaringType은 제네렉 형식 정의의 런타임 형식 핸들이며 handle은 생성된 형식에서 만들어집니다.For example, declaringType is the runtime type handle of the generic type definition, and handle comes from a constructed type.

예제

다음 예제에서는 검색 하는 방법을 보여 줍니다 FieldInfo 생성 된 제네릭 클래스의 필드에 대 한 개체입니다.The following example shows how to retrieve FieldInfo objects for fields on constructed generic classes. 이 예제에서는 제네릭 형식 정의 Test<T> (Test(Of T) Visual basic에서) 라는 단일 필드를 사용 하 여 TestField, 형식의 T합니다.The example defines the generic type Test<T> (Test(Of T) in Visual Basic) with a single field named TestField, of type T. 예제 가져옵니다 합니다 RuntimeFieldHandleRuntimeTypeHandle 경우 여기서 TString, 하 고 다음을 보여 줍니다:The example gets the RuntimeFieldHandle and RuntimeTypeHandle for the case where T is String, and demonstrates the following:

  • 예외가 발생 하는 경우는 GetFieldFromHandle(RuntimeFieldHandle) 메서드 오버 로드를 사용 합니다.An exception is thrown if the GetFieldFromHandle(RuntimeFieldHandle) method overload is used. 필드 형식의 개체인 경우에 마찬가지 T합니다.This is true even if the field is not of type T.

  • A FieldInfo 런타임 형식 핸들 런타임 필드 핸들으로 동일한 구조에서이 경우에 경우 성공적으로 검색 됩니다 Test<string>합니다.A FieldInfo is retrieved successfully if the runtime type handle is from the same construction as the runtime field handle, in this case Test<string>.

  • 런타임 형식 핸들 경우 호환 되는 생성,이 예에서 Test<object>, FieldInfo 호환 생성 시 필드를 검색에 대 한 합니다.If the runtime type handle is from a compatible construction, in this case Test<object>, a FieldInfo for the field on the compatible construction is retrieved.

  • 런타임 형식 핸들 호환 되는 생성에서 없으면 예외가 throw 됩니다.If the runtime type handle is not from a compatible construction, an exception is thrown. 에 대 한 값 형식이 지정 되는 예제의 경우 T합니다.In this case, a value type is specified for 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.

설명

핸들은는 가져온 애플리케이션 도메인 에서만 유효 합니다.Handles are valid only in the application domain in which they were obtained.

하는 것이 좋습니다 declaringType 는 생성 된 런타임 형식 핸들 형식은 항상 handle 에 속합니다.The recommended practice is that declaringType should always be the runtime type handle of the constructed type that handle belongs to. 즉, 경우 handle 에 속하는 필드에 대 한 런타임 필드 핸들 MyType<int> (MyType(Of Integer) Visual Basic의), declaringType 에 대 한 런타임 형식 핸들이 MyType<int>합니다.That is, if handle is a runtime field handle for a field that belongs to MyType<int> (MyType(Of Integer) in Visual Basic), declaringType is the runtime type handle for MyType<int>. 런타임 필드 핸들 제네릭 형식 정의의 필드를 나타내는 경우가 아니면 제네릭 형식 정의의 런타임 형식 핸들을 사용 하지 마세요.Do not use the runtime type handle of the generic type definition, unless the runtime field handle represents a field on the generic type definition.

구현에 따라서는 호환 됩니다.Implementations are compatible in some cases. 예를 들어, 단일 구현은 참조 형식을 제네릭 형식 인수를 사용 하 여 특정 제네릭 형식 정의에서 생성 된 모든 형식에서 공유 됩니다.For example, a single implementation is shared by all types that are constructed from a particular generic type definition by using reference types for the generic type arguments. 예를 들어 MyType<string>하십시오 MyType<object>, 및 MyType<ArrayList> 모두 동일한 구현을 공유 합니다.For example, MyType<string>, MyType<object>, and MyType<ArrayList> all share the same implementation. 이 이런 경우에 FieldInfo 반환 되는 개체 형식에 필드를 나타냅니다는 declaringType 의 원본 소스에 관계 없이 지정 handle합니다.In this situation, the FieldInfo object that is returned represents a field on the type that declaringType specifies, regardless of the original source of handle. 생성 된 형식의 제네릭 형식 인수를 참조 유형은 하는 경우에 작동 하기 때문에이 방법은 권장 되지 않습니다.This practice is not recommended, because it works only if the generic type arguments of the constructed type are reference types.

제네릭 인수 값 형식인 경우 생성 된 형식의 런타임 형식 핸들에서 생성 된 동일한 제네릭 매개 변수 위치에서 참조 형식을 갖는 또는 다른 값 형식을 해당 위치에 있는 런타임 필드 핸들와 호환 되지 않습니다. .If a generic argument is a value type, the runtime type handle of the constructed type is not compatible with runtime field handles from constructions that have a reference type in the same generic parameter position, or that have a different value type in that position. 이런 경우, 사용 하는 유일한 방법은 FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) 오버 로드 되어 있는지 확인 하는 것 declaringType 생성 된 형식에 대 한 런타임 형식 핸들은 handle 에 속합니다.In that case, the only way to use the FieldInfo.GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) overload is to ensure that declaringType is the runtime type handle for the constructed type that handle belongs to.

보안

ReflectionPermission
호출 될 때 런타임에 바인딩된 메커니즘을 통해 같은 InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])합니다.when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 연관된 열거형: MemberAccessAssociated enumeration: MemberAccess.

적용 대상