This documentation is archived and is not being maintained.

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

When overridden in a derived class, sets the property value for the given object to the given value.

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

public abstract void SetValue(
	Object obj,
	Object value,
	BindingFlags invokeAttr,
	Binder binder,
	Object[] index,
	CultureInfo culture


Type: System.Object
The object whose property value will be set.
Type: System.Object
The new value for this property.
Type: System.Reflection.BindingFlags
The invocation attribute. This must be a bit flag from BindingFlags : InvokeMethod, CreateInstance, Static, GetField, SetField, GetProperty, or SetProperty. A suitable invocation attribute must be specified. If a static member is to be invoked, the Static flag of BindingFlags must be set.
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 null, the default binder is used.
Type: System.Object[]
Optional index values for indexed properties. This value should be null for non-indexed properties.
Type: System.Globalization.CultureInfo
The CultureInfo object that represents the culture for which the resource is to be localized. Note that if the resource is not localized for this culture, the CultureInfo.Parent method will be called successively in search of a match. If this value is null, the CultureInfo is obtained from the CultureInfo.CurrentUICulture property.


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


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.


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


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 null, 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.


Starting with the .NET Framework version 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 version 3.5 or later.

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 TestPropertyInfo
    public static void Main()
        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.
        Console.WriteLine("\nGetValue: " + pinfo.GetValue(t, null));

        // Use the SetValue method to change the caption.
        pinfo.SetValue(t, "This caption has been changed.", null);

        //  Display the caption again.
        Console.WriteLine("GetValue: " + pinfo.GetValue(t, null));

        Console.WriteLine("\nPress the Enter key to continue.");

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, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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