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

该方法既不由 obj 的类声明也不由其继承。

示例

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

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

下面的示例检索表示类型的字段FieldsClassFieldInfo 对象的数组,然后调用 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 Service Pack 1 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess,并且非公共成员的授权集仅限于调用方授权集或其子集,则此方法可用于访问非公共成员。 (请参阅 Reflection.) 的安全注意事项

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

适用于

另请参阅