PropertyInfo.SetValue Method (Object, Object, array<Object>[]()[])

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

Syntax

Public Overridable Sub SetValue ( _
    obj As Object, _
    value As Object, _
    index As Object() _
)
public virtual void SetValue(
    Object obj,
    Object value,
    Object[] index
)

Parameters

  • index
    Type: array<System..::.Object>[]()[]
    Optional index values for indexed properties. This value should be nullNothingnullptra null reference (Nothing in Visual Basic) for non-indexed properties.

Exceptions

Exception Condition
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 nullNothingnullptra null reference (Nothing in Visual Basic).

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.

Remarks

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, array<Object>[]()[], CultureInfo) method overload, specifying BindingFlags..::.Default for the BindingFlags parameter, nullNothingnullptra null reference (Nothing in Visual Basic) for Binder, and nullNothingnullptra null reference (Nothing in Visual Basic) 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.

Examples

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

Imports System.Reflection

' Define a class with a property.
Public Class TestClass
   Private myCaption As String = "A Default caption"

   Public Property Caption() As String
      Get
         Return myCaption
      End Get
      Set(ByVal value As String)
         If myCaption <> Value Then myCaption = Value
      End Set
   End Property
End Class

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim t As New TestClass()

      ' Get the type and PropertyInfo.
      Dim myType As Type = t.GetType()
      Dim pinfo As PropertyInfo = myType.GetProperty("Caption")

      ' Display the property value, using the GetValue method.
      outputBlock.Text += String.Format(vbCrLf & "GetValue: " & pinfo.GetValue(t, Nothing)) & vbCrLf

      ' Use the SetValue method to change the caption.
      pinfo.SetValue(t, "This caption has been changed.", Nothing)

      ' Display the caption again.
      outputBlock.Text += String.Format("GetValue: " & pinfo.GetValue(t, Nothing)) & vbCrLf

   End Sub
End Class

' This example produces the following output:
' 
'GetValue: A Default caption
'GetValue: This caption has been changed
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
 */

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

PropertyInfo Class

SetValue Overload

System.Reflection Namespace