Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

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


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


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


The property's set accessor is not found.


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


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


The property is not accessible to the caller.


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 Silverlight, 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; }
         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


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2017 Microsoft