PropertyInfo.SetValue Method

Switch View :
ScriptFree
.NET Framework Class Library
PropertyInfo.SetValue Method

Sets the property value for the given object to the given value.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub SetValue(Object, Object, Object())
[C#] public virtual void SetValue(object, object, object[]);
[C++] public: virtual void SetValue(Object*, Object*, Object*[]);
[JScript] public function SetValue(Object, Object, Object[]);

When overridden in a derived class, sets the property value for the given object to the given value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public MustOverride Sub SetValue(Object, Object, BindingFlags, Binder, Object(), CultureInfo)
[C#] public abstract void SetValue(object, object, BindingFlags, Binder, object[], CultureInfo);
[C++] public: virtual void SetValue(Object*, Object*, BindingFlags, Binder*, Object*[], CultureInfo*) = 0;
[JScript] public abstract function SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo);

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
 */

See Also

PropertyInfo Class | PropertyInfo Members | System.Reflection Namespace