android field get方法,FieldInfo.GetValue(Object) 方法 (System.Reflection) | Microsoft Docs

当在派生类中重写时,返回给定对象支持的字段的值。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);

public abstract object? GetValue (object? obj);

abstract member GetValue : obj -> obj

Public MustOverride Function GetValue (obj As Object) As Object

参数

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

返回

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

实现

例外

该字段是非静态的,并且 obj 是 null。The 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.

调用方没有权限来访问此字段。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 为 null 。Note 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

// hiusing 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

// hiImports 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^ fields = fieldsType->GetFields(static_cast(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 则忽略。If 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 是 Object 。Note 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 开始,此方法可用于访问非公共成员(如果调用方已 ReflectionPermission 使用 ReflectionPermissionFlag.RestrictedMemberAccess 标志授予),并且如果非公共成员的授予集限制为调用方的授予集或其子集,则可使用此方法访问非公共成员。Starting with the .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.

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

适用于

另请参阅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值