PropertyInfo.SetValue Method (Object, Object, Object[])

July 28, 2014

Sets the value of the property on the specified object, with optional index values for indexed properties.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public virtual void SetValue(
	Object obj,
	Object value,
	Object[] index
)

Parameters

obj
Type: System.Object
The object whose property value will be set.
value
Type: System.Object
The new value for this property.
index
Type: System.Object []
Optional index values for indexed properties. This value should be null for non-indexed properties.

ExceptionCondition
ArgumentException

The index array does not contain the type of arguments needed.

-or-

The property's set accessor is not found.

TargetException

The object does not match the target type, or a property is an instance property but obj is null.

TargetParameterCountException

The number of parameters in index does not match the number of parameters the indexed property takes.

MethodAccessException

The property is not accessible to the caller.

TargetInvocationException

An error occurred while setting the property value. For example, an index value specified for an indexed property is out of range. The InnerException property indicates the reason for the error.

To determine whether a property is indexed, use the GetIndexParameters method. If the resulting array has 0 (zero) elements, the property is not indexed.

In Windows Phone, only accessible properties can be set using reflection.

This is a convenience method that calls the SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo) method overload, specifying BindingFlags.Default for the BindingFlags parameter, null for Binder, and null for CultureInfo.

To use the SetValue method, first get a Type object that represents the class. From the Type, get the PropertyInfo. From the PropertyInfo, use the SetValue method.

The following example sets the property value for the specified object to the specified value and displays the result.


using System;
using System.Reflection;

// Define a class with a property.
public class TestClass
{
   private string caption = "A Default caption";
   public string Caption
   {
      get { return caption; }
      set
      {
         if (caption != value)
         {
            caption = value;
         }
      }
   }
}

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      TestClass t = new TestClass();

      // Get the type and PropertyInfo.
      Type myType = t.GetType();
      PropertyInfo pinfo = myType.GetProperty("Caption");

      // Display the property value, using the GetValue method.
      outputBlock.Text += String.Format("\nGetValue: " + pinfo.GetValue(t, null)) + "\n";

      // Use the SetValue method to change the caption.
      pinfo.SetValue(t, "This caption has been changed.", null);

      //  Display the caption again.
      outputBlock.Text += String.Format("GetValue: " + pinfo.GetValue(t, null)) + "\n";
   }
}

/* This example produces the following output:

GetValue: A Default caption
GetValue: This caption has been changed
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft