通过


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

字段标记为文本,但该字段没有接受的文本类型之一。

调用方无权访问此字段。

该方法既不是声明的,也不是由类继承的 obj

示例

以下示例使用 GetValue 该方法检索静态字段的值。 请注意,参数的值 objnull.

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,然后调用GetValue该数组以显示该对象的每个字段fieldsInstFieldsClass的值。

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 应是继承或声明该字段的类的实例。 请注意,返回类型 GetValueObject. 例如,如果字段保存布尔基元值,则返回具有相应布尔值的实例 Object 。 在返回该值之前, GetValue 检查用户是否具有访问权限。

注释

对于完全受信任的代码,将忽略访问限制。 也就是说,每当代码完全受信任时,都可以通过反射访问和调用专用构造函数、方法、字段和属性。

注释

如果使用标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且非公共成员的授予集仅限于调用方授予集或子集,则此方法可用于访问非公共成员。 (请参阅 反射的安全注意事项

若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。

适用于

另请参阅