Returns an array of the public get and set accessors on this property.
[Visual Basic] Overloads MustOverride Public 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[], CultureInfo* culture ) = 0; [JScript] public abstract function SetValue( obj : Object, value : Object, invokeAttr : BindingFlags, binder : Binder, index : Object[], culture : CultureInfo );
Exception Type | Condition |
---|---|
ArgumentException | the index array doesn't contain the exact number and type of arguments needed. |
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.
These implementations both throw NotSupportedException.
[C#]
Make 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; } } Produces the following output Reflection.PropertyInfo GetValue - A Default caption GetValue - This caption has been changed
PropertyInfo Class | PropertyInfo Members | System.Reflection Namespace | PropertyInfo.SetValue Overload List