Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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
)
public abstract void SetValue (
	Object obj, 
	Object value, 
	BindingFlags invokeAttr, 
	Binder binder, 
	Object[] index, 
	CultureInfo culture
)
public abstract function SetValue (
	obj : Object, 
	value : Object, 
	invokeAttr : BindingFlags, 
	binder : Binder, 
	index : Object[], 
	culture : CultureInfo
)

Parameters

obj

The object whose property value will be returned.

value

The new value for this property.

invokeAttr

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.

binder

An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects through reflection. If binder is a null reference (Nothing in Visual Basic), the default binder is used.

index

Optional index values for indexed properties. This value should be a null reference (Nothing in Visual Basic) for non-indexed properties.

culture

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 a null reference (Nothing in Visual Basic), the CultureInfo is obtained from the CultureInfo.CurrentUICulture property.

Exception typeCondition

ArgumentException

The index array does not contain the type of arguments needed.

-or-

The property's Get method is not found.

TargetException

The object does not match the target type, or a property is an instance property but obj is a null reference (Nothing in Visual Basic).

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.

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.

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; }
        set 
        { 
            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.");
        Console.ReadLine();
    }
}

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

Press the Enter key to continue.
*/

import System.*;
import System.Reflection.*;

// Define a class with a property.
public class TestClass
{
    private String caption = "A Default caption";

    /** @property 
     */
    public String get_Caption()
    {
        return caption ;
    } //get_Caption

    /** @property 
     */
    public void set_Caption (String value)
    {
        if (caption != value) {
            caption = value;
        }
    } //set_Caption
} //TestClass

class TestPropertyInfo
{   
   public static void main(String[] args)
   {
        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.");
        Console.ReadLine();
   } //main
} //TestPropertyInfo

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

Press the Enter key to continue.
*/

import System;
import System.Reflection;
 
// Define a class with a property.
public class TestClass
{
    private var caption : String = "A Default caption";
    public function get Caption() : String {
        return caption;
    }
    public function set Caption(value:String) {
        if(caption!=value) caption = value;
    }
}
 
public class TestPropertyInfo
{
    public static function Main() : void
    {
        var t : TestClass = new TestClass();
 
        // Get the type and the PropertyInfo.
        var myType : Type = t.GetType();
        var pinfo : PropertyInfo = myType.GetProperty("Caption");
 
        // Display the property, 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.");
        Console.ReadLine();
    }
}

TestPropertyInfo.Main();

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

Press the Enter key to continue.
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft