FieldInfo.SetValue 方法 (Object, Object)

2013/12/13

设置给定对象支持的字段值。

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

public void SetValue(
	Object obj,
	Object value
)

参数

obj
类型: System.Object
将设置其字段值的对象。
value
类型: System.Object
分配给字段的值。

异常条件
FieldAccessException

该字段不可访问。

TargetException

obj 参数为 null 并且该字段是一个实例字段。

ArgumentException

对象上不存在该字段。

- 或 -

value 参数无法转换并存储在该字段中。

MethodAccessException

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

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

此方法将 value 分配给此实例在 obj 对象上反映的字段。如果该字段是静态的,则将忽略 obj。对于非静态字段,obj 应是继承或声明该字段的类的实例。新值作为 Object 传递。例如,如果字段属于 Boolean 类型,则必须将新值作为 Object 的实例装箱。

版本说明

Windows Phone

 当 objnull 时,SetValue 引发 ArgumentNullException 异常而不是 TargetException 异常。

如果在一个同时定义为公用和常量的字段上调用 SetValue 方法,将引发 MemberAccessException 异常而不是 FieldAccessException 异常。

下面的示例获取 Example 类的每个字段的值,通过向值追加一个字符串来修改该值,从而设置新值。然后检索新值,并显示结果。如果由于字段的访问级别而无法检索字段值,将捕获异常并显示一条消息。在此示例中,由于 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 change their values. This does not 
        // require an instance of Test, so Nothing is passed to SetValue.
        FieldInfo[] sharedFields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | 
                                               BindingFlags.Static);
        foreach( FieldInfo f in sharedFields )
        {
            try
            {
                // Append a marker to the old value.
                string newValue = f.GetValue(null) + " -modified-";

                f.SetValue(null, newValue);

                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 change their values for the instance 
        // created earlier.
        FieldInfo[] instanceFields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | 
                                                 BindingFlags.Instance);
        foreach( FieldInfo f in instanceFields )
        {
            try
            {
                // Append a marker to the old value.
                string newValue = f.GetValue(myInstance) + " -modified-";

                f.SetValue(myInstance, newValue);

                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. -modified-
The value of Shared field SB is: A friend shared field. -modified-
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. -modified-
The value of instance field B is: A friend instance field. -modified-
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

显示:
© 2014 Microsoft