PropertyInfo.SetValue Method (Object, Object)

.NET Framework (current version)

Sets the property value of a specified object.

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

public void SetValue(
	object obj,
	object value


Type: System.Object

The object whose property value will be set.

Type: System.Object

The new property value.

Exception Condition

The property's set accessor is not found.


value cannot be converted to the type of PropertyType.


The type of obj does not match the target type, or a property is an instance property but obj is null.


In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MemberAccessException, instead.

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


An error occurred while setting the property value. The Exception.InnerException property indicates the reason for the error.

The SetValue(Object, Object) overload sets the value of a non-indexed property. To determine whether a property is indexed, call the GetIndexParameters method. If the resulting array has 0 (zero) elements, the property is not indexed. To set the value of an indexed property, call the SetValue(Object, Object, Object[]) overload.

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

This is a convenience method that calls the runtime implementation of the abstract SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo) method, specifying BindingFlags.Default for the BindingFlags parameter, null for Binder, null for Object[], and null for CultureInfo.

To use the SetValue method, first get a Type object that represents the class. From the Type, get the PropertyInfo object. From the PropertyInfo object, call the SetValue method.


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 declares a class named Example with one static (Shared in Visual Basic) and one instance property. The example uses the SetValue(Object, Object) method to change the original property values and displays the original and final values.

using System;
using System.Reflection;

class Example
    private static int _staticProperty = 41;
    private int _instanceProperty = 42;

    // Declare a public static property.
    public static int StaticProperty
        get { return _staticProperty; }
        set { _staticProperty = value; }

    // Declare a public instance property.
    public int InstanceProperty
        get { return _instanceProperty; }
        set { _instanceProperty = value; }

    public static void Main()
        Console.WriteLine("Initial value of static property: {0}",

        // Get a type object that represents the Example type.
        Type examType = typeof(Example);

        // Change the static property value.
        PropertyInfo piShared = examType.GetProperty("StaticProperty");
        piShared.SetValue(null, 76);

        Console.WriteLine("New value of static property: {0}",

        // Create an instance of the Example class.
        Example exam = new Example();

        Console.WriteLine("\nInitial value of instance property: {0}", 

        // Change the instance property value.
        PropertyInfo piInstance = examType.GetProperty("InstanceProperty");
        piInstance.SetValue(exam, 37);

        Console.WriteLine("New value of instance property: {0}",
// The example displays the following output:
//       Initial value of static property: 41
//       New value of static property: 76
//       Initial value of instance property: 42
//       New value of instance property: 37


for accessing non-public members when the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. Associated enumeration: ReflectionPermissionFlag.RestrictedMemberAccess


for accessing non-public members regardless of their grant set. Associated enumeration: ReflectionPermissionFlag.MemberAccess


when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top