Windows Dev Center

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)

'Declaration
Public Overridable Sub SetValue ( _
	obj As Object, _
	value As Object, _
	index As Object() _
)

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 Nothing 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 Nothing.

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, Nothing for Binder, and Nothing 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.


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft