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, Object[])

Sets the value of the property with optional index values for index properties.

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

public virtual void SetValue (
	Object obj,
	Object value,
	Object[] index
)
public void SetValue (
	Object obj, 
	Object value, 
	Object[] index
)
public function SetValue (
	obj : Object, 
	value : Object, 
	index : Object[]
)
Not applicable.

Parameters

obj

The object whose property value will be set.

value

The new value for this property.

index

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

Exception typeCondition

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

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.

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, a null reference (Nothing in Visual Basic) for Binder, and a null reference (Nothing in Visual Basic) for CultureInfo.

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

NoteNote:

Starting with the , this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit 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 . For more information, see The .NET Framework 3.5 Architecture.

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2015 Microsoft