FieldInfo.GetValue(Object) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
파생 클래스에서 재정의되는 경우 지정된 개체에서 지원하는 필드의 값을 반환합니다.
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 합니다.
필드는 리터럴로 표시되어 있지만 필드에 허용되는 리터럴 형식 중 하나가 없습니다.
호출자에게 이 필드에 액세스할 수 있는 권한이 없습니다.
참고: .NET Windows 스토어 앱 또는 보고 가능한 클래스 라이브러리 대신 기본 클래스 예외인 MemberAccessException catch합니다.
메서드는 선언되거나 해당 클래스 obj에서 상속되지 않습니다.
예제
다음 예제에서는 메서드를 GetValue 사용하여 정적 필드의 값을 검색합니다. 인수의 obj 값은 .입니다 null.
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
다음 예제에서는 형식의 필드를 나타내는 개체의 FieldInfo 배열을 FieldsClass 검색한 다음 GetValue 호출하여 개체에 대한 fieldsInst 각 필드의 값을 표시합니다.
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 반환하기 전에 사용자에게 액세스 권한이 있는지 확인합니다.
메모
완전히 신뢰할 수 있는 코드에 대한 액세스 제한은 무시됩니다. 즉, 코드를 완전히 신뢰할 때마다 리플렉션을 통해 프라이빗 생성자, 메서드, 필드 및 속성에 액세스하고 호출할 수 있습니다.
메모
이 메서드는 호출자가 플래그를 ReflectionPermission 사용하여 부여된 ReflectionPermissionFlag.RestrictedMemberAccess 경우 및 비공용 멤버의 권한 부여 집합이 호출자의 권한 부여 집합 또는 해당 하위 집합으로 제한되는 경우 비공용 멤버에 액세스하는 데 사용할 수 있습니다. ( 리플렉션에 대한 보안 고려 사항 참조)
이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.