次の方法で共有


PropertyInfo.SetValue メソッド

指定したオブジェクトのプロパティ値に、指定した値を設定します。

オーバーロードの一覧

プロパティの値を設定します。インデックス付きプロパティの場合は、オプションでインデックス値を設定できます。

.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[]);

派生クラスによってオーバーライドされた場合に、指定したオブジェクトのプロパティ値を指定した値に設定します。

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

使用例

指定したオブジェクトに対するプロパティの値を指定した値に設定し、その結果を表示する例を次に示します。

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

参照

PropertyInfo クラス | PropertyInfo メンバ | System.Reflection 名前空間