PropertyInfo::SetValue Method (Object^, Object^)
Sets the property value of a specified object.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- obj
-
Type:
System::Object^
The object whose property value will be set.
- value
-
Type:
System::Object^
The new property value.
| Exception | Condition | ||
|---|---|---|---|
| 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 |
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^, array<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^, array<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.
Note |
|---|
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 namespace System; using namespace System::Reflection; ref class Example { private: int static _sharedProperty = 41; int _instanceProperty; public: Example() { _instanceProperty = 42; }; static property int SharedProperty { int get() { return _sharedProperty; } void set(int value) { _sharedProperty = value; } }; property int InstanceProperty { int get() { return _instanceProperty; } void set(int value) { _instanceProperty = value; } }; }; void main() { Console::WriteLine("Initial value of static property: {0}", Example::SharedProperty); PropertyInfo^ piShared = Example::typeid->GetProperty("SharedProperty"); piShared->SetValue(nullptr, 76, nullptr); Console::WriteLine("New value of static property: {0}", Example::SharedProperty); Example^ exam = gcnew Example(); Console::WriteLine("\nInitial value of instance property: {0}", exam->InstanceProperty); PropertyInfo^ piInstance = Example::typeid->GetProperty("InstanceProperty"); piInstance->SetValue(exam, 37, nullptr); 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 */
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.
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
