FieldInfo.GetValue(Object) 메서드

정의

파생 클래스에서 재정의된 경우 지정된 개체에서 지원하는 필드의 값을 반환합니다.

public:
 abstract System::Object ^ GetValue(System::Object ^ obj);
public abstract object GetValue (object obj);
public abstract object? GetValue (object? obj);
abstract member GetValue : obj -> obj
Public MustOverride Function GetValue (obj As Object) As Object

매개 변수

obj
Object

필드 값이 반환될 개체입니다.

반환

이 인스턴스에서 반영한 필드의 값을 포함하는 개체입니다.

구현

예외

필드가 비정적이며 objnull인 경우

참고: Windows 스토어 앱 또는 이식 가능한 클래스 라이브러리용 .NET에서 대신 catch Exception 합니다.

필드가 리터럴로 표시되지만 필드에 허용된 리터럴 형식이 하나도 없는 경우

호출자에게 이 필드에 액세스할 수 있는 권한이 없는 경우

참고: Windows 스토어 앱 또는 이식 가능한 클래스 라이브러리용 .NET에서 기본 클래스 예외 를 MemberAccessException대신 catch합니다.

obj 클래스에서 메서드를 선언하지도 않고 상속하지도 않은 경우

예제

다음 예제에서는 메서드를 GetValue 사용하여 정적 필드의 값을 검색합니다. 인수의 값은 obj 입니다 null.

using namespace System;
using namespace System::Reflection;

ref class Example
{
  public:
     static String^ val = "test";
};

int main()
{
   FieldInfo^ fld = Example::typeid->GetField( "val" );
   Console::WriteLine(fld->GetValue(nullptr) );
   Example::val = "hi";
   Console::WriteLine(fld->GetValue(nullptr) );
}
// The example displays the following output:
//     test
//     hi
using System;
using System.Reflection;

class Example
{
    public static String val = "test";

    public static void Main()
    {
        FieldInfo fld = typeof(Example).GetField("val");
        Console.WriteLine(fld.GetValue(null));
        val = "hi";
        Console.WriteLine(fld.GetValue(null));
    }
}
// The example displays the following output:
//     test
//     hi
Imports System.Reflection

Class Example
    Public Shared val As String = "test"
    
    Public Shared Sub Main()
        Dim fld As FieldInfo = GetType(Example).GetField("val")
        Console.WriteLine(fld.GetValue(Nothing))
        val = "hi"
        Console.WriteLine(fld.GetValue(Nothing))
    End Sub 
End Class 
' The example displays the following output:
'     test
'     hi

다음 예제에서는 형식의 필드를 FieldsClass 나타내는 개체의 FieldInfo 배열을 검색한 다음 를 호출 GetValue 하여 개체에 대한 fieldsInst 각 필드의 값을 표시합니다.

using namespace System;
using namespace System::Reflection;

public ref class FieldsClass
{
  public:
     String^ fieldA;
     String^ fieldB;

     FieldsClass()
     {
        fieldA = "A public field";
        fieldB = "Another public field";
     }
};

int main()
{
   FieldsClass^ fieldsInst = gcnew FieldsClass;
   
   // Get the type of FieldsClass.
   Type^ fieldsType = FieldsClass::typeid;

   // Get the FieldInfo of FieldsClass.
   array<FieldInfo^>^ fields = fieldsType->GetFields(static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));

   // Display the values of the fields.
   Console::WriteLine("Displaying the values of the fields of {0}:", fieldsType);
   for (int i = 0; i < fields->Length; i++)
   {
      Console::WriteLine("   {0}:\t'{1}'", 
                         fields[i]->Name, fields[i]->GetValue(fieldsInst));
   }
}
// The example displays the following output:
//     Displaying the values of the fields of FieldsClass:
//        fieldA:      'A public field'
//        fieldB:      'Another public field'
using System;
using System.Reflection;

public class FieldsClass
{
    public string fieldA;
    public string fieldB;

    public FieldsClass()
    {
        fieldA = "A public field";
        fieldB = "Another public field";
    }
}

public class Example
{
    public static void Main()
    {
        FieldsClass fieldsInst = new FieldsClass();
        // Get the type of FieldsClass.
        Type fieldsType = typeof(FieldsClass);

        // Get an array of FieldInfo objects.
        FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public
            | BindingFlags.Instance);
        // Display the values of the fields.
        Console.WriteLine("Displaying the values of the fields of {0}:",
            fieldsType);
        for(int i = 0; i < fields.Length; i++)
        {
            Console.WriteLine("   {0}:\t'{1}'",
                fields[i].Name, fields[i].GetValue(fieldsInst));
        }
    }
}
// The example displays the following output:
//     Displaying the values of the fields of FieldsClass:
//        fieldA:      'A public field'
//        fieldB:      'Another public field'
Imports System.Reflection

Public Class FieldsClass
    Public fieldA As String
    Public fieldB As String

    Public Sub New()
        fieldA = "A public field"
        fieldB = "Another public field"
    End Sub 
End Class 

Public Module Example
    Public Sub Main()
        Dim fieldsInst As New FieldsClass()
        ' Get the type of FieldsClass.
        Dim fieldsType As Type = GetType(FieldsClass)

        ' Get an array of FieldInfo objects.
        Dim fields As FieldInfo() = fieldsType.GetFields(BindingFlags.Public Or BindingFlags.Instance)
        ' Display the values of the fields.
        Console.WriteLine("Displaying the values of the fields of {0}:", fieldsType)
        For i As Integer = 0 To fields.Length - 1
            Console.WriteLine("   {0}:{2}'{1}'",
                fields(i).Name, fields(i).GetValue(fieldsInst), vbTab)
        Next 
    End Sub 
End Module
' The example displays the following output:
'     Displaying the values of the fields of FieldsClass:
'        fieldA:      'A public field'
'        fieldB:      'Another public field'

설명

필드가 정적 obj 이면 가 무시됩니다. 비정적 필드의 경우 은 필드를 obj 상속하거나 선언하는 클래스의 인스턴스여야 합니다. 의 GetValue 반환 형식은 입니다 Object. 예를 들어 필드에 부울 기본값이 있는 경우 적절한 부울 값이 있는 인스턴스 Object 가 반환됩니다. 값을 GetValue 반환하기 전에 사용자에게 액세스 권한이 있는지 확인합니다.

참고

완전히 신뢰할 수 있는 코드에 대한 액세스 제한은 무시됩니다. 즉, 코드를 완전히 신뢰할 때마다 리플렉션을 통해 프라이빗 생성자, 메서드, 필드 및 속성에 액세스하고 호출할 수 있습니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 메서드는 호출자가 플래그를 ReflectionPermissionFlag.RestrictedMemberAccess 사용하여 부여된 ReflectionPermission 경우 및 비공용 멤버의 권한 부여 집합이 호출자의 권한 부여 집합 또는 해당 하위 집합으로 제한되는 경우 비공용 멤버에 액세스하는 데 사용할 수 있습니다. ( 리플렉션에 대한 보안 고려 사항을 참조하세요.)

이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상

추가 정보