次の方法で共有


PropertyInfo.SetValue メソッド (Object, Object, BindingFlags, Binder, Object , CultureInfo)

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

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(objectobj,objectvalue,BindingFlagsinvokeAttr,Binderbinder,object[] index,CultureInfoculture);
[C++]
public: virtual void SetValue(Object* obj,Object* value,BindingFlagsinvokeAttr,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);

パラメータ

  • obj
    プロパティ値が返されるオブジェクト。
  • value
    このプロパティの新しい値。
  • invokeAttr
    呼び出し属性。 BindingFlags のビット フラグ InvokeMethodCreateInstanceStaticGetFieldSetFieldGetProperty 、または SetProperty にする必要があります。適切な呼び出し属性を指定する必要があります。静的メンバを呼び出す場合は、 BindingFlagsStatic フラグを設定する必要があります。
  • binder
    バインディング、引数型の強制変換、メンバの呼び出し、およびリフレクションを使用した MemberInfo オブジェクトの取得を有効にするオブジェクト。binder が null 参照 (Visual Basic では Nothing) の場合は、既定のバインダが使用されます。
  • index
    インデックス付きプロパティのインデックス値 (省略可能)。インデックス付きでないプロパティの場合は、この値を null 参照 (Visual Basic では Nothing) にする必要があります。
  • culture
    リソースのローカライズ対象のカルチャを表す CultureInfo オブジェクト。リソースがこのカルチャ用にローカライズされていない場合は、一致する対象を検索するために CultureInfo.Parent メソッドが連続して呼び出されます。この値が null 参照 (Visual Basic では Nothing) の場合は、 CultureInfo.CurrentUICulture プロパティから CultureInfo が取得されます。

例外

例外の種類 条件
ArgumentException index 配列に、必要な引数の型が格納されていません。

または

プロパティの Get メソッドが見つかりません。

TargetException オブジェクトが対象の型と一致しないか、プロパティがインスタンス プロパティであるが、 obj が null 参照 (Visual Basic では Nothing) です。
TargetParameterCountException index 内のパラメータ数が、インデックス付きプロパティのパラメータ数と一致しません。
MethodAccessException クラス内のプライベート メソッドまたはプロテクト メソッドに無効なアクセスが試行されました。

解説

完全に信頼されたコードでは、アクセス制限は無視されます。コードが完全に信頼されていると、リフレクションを使用して、プライベートなコンストラクタ、メソッド、フィールド、およびプロパティにアクセスし、それらを呼び出すことができます。

SetValue メソッドを使用するには、最初に Type クラスを取得します。そして、その Type から PropertyInfo を取得します。 PropertyInfo から、 SetValue メソッドを使用します。

使用例

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

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

.NET Framework セキュリティ:

参照

PropertyInfo クラス | PropertyInfo メンバ | System.Reflection 名前空間 | PropertyInfo.SetValue オーバーロードの一覧