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.
[Visual Basic] Overloads Public MustOverride Sub SetValue( _ ByVal obj As Object, _ ByVal value As Object, _ ByVal invokeAttr As BindingFlags, _ ByVal binder As Binder, _ ByVal index() As Object, _ ByVal culture As CultureInfo _ ) [C#] public abstract void SetValue( object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture ); [C++] public: virtual void SetValue( Object* obj, Object* value, BindingFlags invokeAttr, Binder* binder, Object* index __gc[], CultureInfo* culture ) = 0; [JScript] 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.
Exceptions
| Exception Type | Condition |
|---|---|
| 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. |
Remarks
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.
Example
The following example sets the property value for the specified object to the specified value and displays the result.
[Visual Basic] Imports System Imports System.Reflection Imports Microsoft.VisualBasic ' Define a property. Public Class Myproperty Private myCaption As String = "A Default caption" Public Property Caption() As String Get Return myCaption End Get Set(ByVal Value As String) If myCaption <> value Then myCaption = value End If End Set End Property End Class Class Mypropertyinfo Public Shared Function Main() As Integer Console.WriteLine(ControlChars.CrLf & "Reflection.PropertyInfo") Dim Myproperty As New Myproperty() ' Get the type and PropertyInfo. Dim MyType As Type = Type.GetType("Myproperty") Dim Mypropertyinfo As PropertyInfo = MyType.GetProperty("Caption") ' Get and display the GetValue method. Console.WriteLine("GetValue - " & _ Mypropertyinfo.GetValue(Myproperty, Nothing).ToString()) ' Use the SetValue method to change the caption. Mypropertyinfo.SetValue(Myproperty, "This caption has been changed.", _ Nothing) ' Get the caption again and display it. Console.WriteLine("GetValue - " & _ Mypropertyinfo.GetValue(Myproperty, Nothing).ToString()) Return 0 End Function End Class [C#] using System; using System.Reflection; // Define a property. public class Myproperty { private string caption = "A Default caption"; public string Caption { get{return caption;} set {if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main() { Console.WriteLine ("\nReflection.PropertyInfo"); Myproperty Myproperty = new Myproperty(); // Get the type and PropertyInfo. Type MyType = Type.GetType("Myproperty"); PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption"); // Get and display the GetValue method. Console.Write ("\nGetValue - " + Mypropertyinfo.GetValue (Myproperty, null)); // Use the SetValue method to change the caption. Mypropertyinfo.SetValue( Myproperty, "This caption has been changed.", null); // Get the caption again and display it. Console.Write ("\nGetValue - " + Mypropertyinfo.GetValue (Myproperty, null)); return 0; } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Reflection; // Define a property. public __gc class Myproperty { private: String* caption; public: Myproperty() : caption(S"A Default caption") {} __property String* get_Caption() { return caption; } __property void set_Caption(String* value) { if(caption!=value) { caption = value; } } }; int main() { Console::WriteLine (S"\nReflection.PropertyInfo"); Myproperty* myproperty = new Myproperty(); // Get the type and PropertyInfo. Type* MyType = Type::GetType(S"Myproperty"); PropertyInfo* Mypropertyinfo = MyType->GetProperty(S"Caption"); // Get and display the GetValue method. Console::Write (S"\nGetValue - {0}", Mypropertyinfo->GetValue(myproperty,0)); // Use the SetValue method to change the caption. Mypropertyinfo->SetValue( myproperty, S"This caption has been changed.", 0); // Get the caption again and display it. Console::Write (S"\nGetValue - {0}", Mypropertyinfo->GetValue(myproperty,0)); return 0; } [JScript] import System; import System.Reflection; //Make a property public class Myproperty { 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; } } class Mypropertyinfo { public static function Main() : void { Console.WriteLine ("\nReflection.PropertyInfo"); var myproperty : Myproperty = new Myproperty(); //Get the type and PropertyInfo var MyType : Type = Type.GetType("Myproperty"); var Mypropertyinfo : PropertyInfo = MyType.GetProperty("Caption"); //Get and display the GetValue Method Console.Write ("\nGetValue - " + Mypropertyinfo.GetValue (myproperty, null)); //Use the SetValue Method to change the caption Mypropertyinfo.SetValue( myproperty, "This caption has been changed", null); //Get the caption again and display it Console.Write ("\nGetValue - " + Mypropertyinfo.GetValue (myproperty, null)); } } Mypropertyinfo.Main(); /* Produces the following output Reflection.PropertyInfo GetValue - A Default caption GetValue - This caption has been changed */
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
.NET Framework Security:
- ReflectionPermission when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.
- ReflectionPermission for reflecting non-public objects. Associated enumerations: ReflectionPermissionFlag.MemberAccess, ReflectionPermissionFlag.TypeInformation
See Also
PropertyInfo Class | PropertyInfo Members | System.Reflection Namespace | PropertyInfo.SetValue Overload List