Export (0) Print
Expand All

PropertyInfo.SetValue Method (Object, Object)

.NET Framework 4.5

Sets the property value of a specified object.

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

[DebuggerStepThroughAttribute]
[DebuggerHiddenAttribute]
public void SetValue(
	Object obj,
	Object value
)

Parameters

obj
Type: System.Object

The object whose property value will be set.

value
Type: System.Object

The new property value.

ExceptionCondition
ArgumentException

The property's set accessor is not found.

-or-

value cannot be converted to the type of PropertyType.

TargetException

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

MethodAccessException
NoteNote

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.

TargetInvocationException

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.

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 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}",
            Example.StaticProperty);

        // 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}",
                          Example.StaticProperty);

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

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

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

        Console.WriteLine("New value of instance property: {0}",
                          exam.InstanceProperty);
    }
}
// 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

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Show:
© 2015 Microsoft