Freigeben über


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

'Declaration
<DebuggerStepThroughAttribute> _
<DebuggerHiddenAttribute> _
Public Overridable Sub SetValue ( _
    obj As Object, _
    value As Object, _
    index As Object() _
)
[DebuggerStepThroughAttribute]
[DebuggerHiddenAttribute]
public virtual void SetValue(
    Object obj,
    Object value,
    Object[] index
)

Parameters

  • obj
    Type: System.Object
    The object whose property value will be set.
  • index
    Type: array<System.Object[]
    Optional index values for indexed properties. This value should be nulla 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 nulla 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 Silverlight, 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, nulla null reference (Nothing in Visual Basic) for Binder, and nulla 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

Silverlight

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

Platforms

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