信息
您所需的主题如下所示。但此主题未包含在此库中。

FieldInfo.GetValue 方法

2013/12/13

在派生类中被重写时,返回给定对象支持的字段的值。

Namespace:  System.Reflection
程序集:  mscorlib(位于 mscorlib.dll 中)

public abstract Object GetValue(
	Object obj
)

参数

obj
类型: System.Object
其字段值将返回的对象。

返回值

类型: System.Object
包含此实例反映的字段值的对象。

异常条件
TargetException

此字段是非静态的且 objnull

NotSupportedException

字段被标记为文本,但是该字段没有一个可接受的文本类型。

FieldAccessException

该字段不可访问。

ArgumentException

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

MethodAccessException

通过如 Type.InvokeMember 等机制以后期绑定的方式调用该成员。

在 Windows Phone 应用中,只有可访问的字段可通过反射来检索。

如果该字段是静态的,则忽略 obj。对于非静态字段,obj 应是继承或声明该字段的类的实例。请注意,GetValue 的返回类型为 Object。例如,如果字段属于 Boolean 类型,则其值将装箱并作为 Object 的实例返回。

版本说明

Windows Phone

当 null 作为第一个参数传递时,GetValue 将引发 ArgumentNullException

下面的示例检索 Test 类的所有字段,并显示字段值。如果由于字段的访问级别而无法检索字段值,将捕获异常并显示一条消息。在此示例中,由于 ExampleTest 类在同一程序集内,因此可访问 internal 字段(在 Visual Basic 中为 Friend 字段)。

说明注意:

要运行此示例,请参见生成具有静态 Windows Phone TextBlock 控件的示例


using System;
using System.Reflection;

public class Test
{
    public static string SA = "A public shared field.";
    internal static string SB = "A friend shared field.";
    protected static string SC = "A protected shared field.";
    private static string SD = "A private shared field.";

    public string A = "A public instance field.";
    internal string B = "A friend instance field.";
    protected string C = "A protected instance field.";
    private string D = "A private instance field.";
}

public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        // Create an instance of Test, and get a Type object.
        Test myInstance = new Test();
        Type t = typeof(Test);

        // Get the shared fields of Test, and display their values. This does not 
        // require an instance of Test, so null is passed to GetValue.
        FieldInfo[] sharedFields = 
            t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
        foreach( FieldInfo f in sharedFields )
        {
            try
            {
                outputBlock.Text += 
                    String.Format("The value of Shared field {0} is: {1}\n", 
                                  f.Name, 
                                  f.GetValue(null));
            }
            catch
            {
                outputBlock.Text += 
                    String.Format("The value of Shared field {0} is not accessible.\n", 
                                  f.Name);
            }
        }

        // Get the instance fields of Test, and display their values for the instance 
        // created earlier.
        FieldInfo[] instanceFields = 
            t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        foreach( FieldInfo f in instanceFields )
        {
            try
            {
                outputBlock.Text += String.Format("The value of instance field {0} is: {1}\n", 
                                                  f.Name, 
                                                  f.GetValue(myInstance));
            }
            catch
            {
                outputBlock.Text += 
                    String.Format("The value of instance field {0} is not accessible.\n", 
                                  f.Name);
            }
        }
    }
}

/* This example produces the following output:

The value of Shared field SA is: A public shared field.
The value of Shared field SB is: A friend shared field.
The value of Shared field SC is not accessible.
The value of Shared field SD is not accessible.
The value of instance field A is: A public instance field.
The value of instance field B is: A friend instance field.
The value of instance field C is not accessible.
The value of instance field D is not accessible.
 */


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: