PropertyInfo.SetValue Method (Object, Object, BindingFlags, Binder, Object(), CultureInfo)

When overridden in a derived class, sets the property value for a specified object that has the specified binding, index, and culture-specific information.

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

'Declaration
Public MustOverride Sub SetValue ( _
	obj As Object, _
	value As Object, _
	invokeAttr As BindingFlags, _
	binder As Binder, _
	index As Object(), _
	culture As CultureInfo _
)

Parameters

obj
Type: System.Object

The object whose property value will be set.

value
Type: System.Object

The new property value.

invokeAttr
Type: System.Reflection.BindingFlags

A bitwise combination of the following enumeration members that specify the invocation attribute: InvokeMethod, CreateInstance, Static, GetField, SetField, GetProperty, or SetProperty. You must specify a suitable invocation attribute. For example, to invoke a static member, set the Static flag.

binder
Type: System.Reflection.Binder

An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects through reflection. If binder is Nothing, the default binder is used.

index
Type: System.Object()

Optional index values for indexed properties. This value should be Nothing for non-indexed properties.

culture
Type: System.Globalization.CultureInfo

The culture for which the resource is to be localized. If the resource is not localized for this culture, the CultureInfo.Parent property will be called successively in search of a match. If this value is Nothing, the culture-specific information is obtained from the CultureInfo.CurrentUICulture property.

Implements

_PropertyInfo.SetValue(Object, Object, BindingFlags, Binder, Object(), CultureInfo)

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

There was an illegal attempt to access a private or protected method inside a class.

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.

If this PropertyInfo object is a value type and value is Nothing, then the property will be set to the default value for that type.

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

Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked via Reflection whenever the code is fully trusted.

To use the SetValue method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the SetValue method.

NoteNote

Starting with the .NET Framework 2.0 Service Pack 1, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller’s grant set, or a subset thereof. (See Security Considerations for Reflection.)

To use this functionality, your application should target the .NET Framework 3.5 or later.

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

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' 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 
            If myCaption <> value Then myCaption = value
        End Set 
    End Property 
End Class 

Public Class TestPropertyInfo
    Public Shared Sub Main()
        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.
        Console.WriteLine(vbCrLf & "GetValue: " & pinfo.GetValue(t, Nothing))

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

        ' Display the caption again.
        Console.WriteLine("GetValue: " & pinfo.GetValue(t, Nothing))

        Console.WriteLine(vbCrLf & "Press the Enter key to continue.")
        Console.ReadLine()
    End Sub 
End Class 

' This example produces the following output: 
'  
'GetValue: A Default caption 
'GetValue: This caption has been changed 

'Press the Enter key to continue.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft