FieldInfo.GetValue(Object) 方法

定义

当在派生类中重写时,返回给定对象支持的字段的值。When overridden in a derived class, returns the value of a field supported by a given object.

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

参数

obj
Object

将返回其字段值的对象。The object whose field value will be returned.

返回

一个对象,包含此实例反映的字段的值。An object containing the value of the field reflected by this instance.

实现

异常

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获 ExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch Exception instead.

该字段是非静态的,并且 objnullThe field is non-static and obj is null.

字段被标记为文本,但该字段不具有一个可被接受的文本类型。A field is marked literal, but the field does not have one of the accepted literal types.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 MemberAccessExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MemberAccessException, instead.

调用方没有权限来访问此字段。The caller does not have permission to access this field.

该方法既不由 obj 的类声明也不由其继承。The method is neither declared nor inherited by the class of obj.

示例

下面的示例使用 GetValue 方法检索静态字段的值。The following example uses the GetValue method to retrieve the value of a static field. 请注意,obj 参数的值是 nullNote that the value of the obj argument is 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

下面的示例检索一个 FieldInfo 对象的数组,这些对象表示 FieldsClass 类型的字段,然后调用 GetValue 以显示 fieldsInst 对象的每个字段的值。The following example retrieves an array of FieldInfo objects that represents the fields of the FieldsClass type, and then calls the GetValue to display the value of each field for the fieldsInst object.

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'

注解

如果该字段是静态的,则忽略 objIf the field is static, obj is ignored. 对于非静态字段,obj 应是继承或声明该字段的类的实例。For non-static fields, obj should be an instance of a class that inherits or declares the field. 请注意,GetValue 的返回类型为 ObjectNote that the return type of GetValue is Object. 例如,如果该字段包含一个布尔值基元值,则返回一个具有适当布尔值的 Object 的实例。For example, if the field holds a Boolean primitive value, an instance of Object with the appropriate Boolean value is returned. 在返回该值之前,GetValue 会检查用户是否具有访问权限。Before returning the value, GetValue checks to see if the user has access permission.

备注

对于完全受信任的代码,将忽略访问限制。Access restrictions are ignored for fully trusted code. 也就是说,只要代码完全受信任,就可以通过反射访问和调用私有构造函数、方法、字段和属性。That is, private constructors, methods, fields, and properties can be accessed and invoked through reflection whenever the code is fully trusted.

备注

.NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1开始,此方法可用于访问非公共成员(如果已向调用方授予了 ReflectionPermissionFlag.RestrictedMemberAccess 标记 ReflectionPermission,以及非公共成员的授予集限制为调用方的授予集或其子集)。Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. (请参阅反射的安全注意事项。)(See Security Considerations for Reflection.)

若要使用此功能,应用程序应为 .NET Framework 3.5.NET Framework 3.5 或更高版本。To use this functionality, your application should target the .NET Framework 3.5.NET Framework 3.5 or later.

安全性

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.

适用于

另请参阅