次の方法で共有


FieldInfo.SetValue メソッド (Object, Object)

指定したオブジェクトでサポートされているフィールドの値を設定します。

Overloads Public Sub SetValue( _
   ByVal obj As Object, _   ByVal value As Object _)
[C#]
public void SetValue(objectobj,objectvalue);
[C++]
public: void SetValue(Object* obj,Object* value);
[JScript]
public function SetValue(
   obj : Object,value : Object);

パラメータ

  • obj
    フィールド値が設定されるオブジェクト。
  • value
    フィールドに代入する値。

例外

例外の種類 条件
FieldAccessException 呼び出し元に、このフィールドに対するアクセス許可がありません。
TargetException obj パラメータが null 参照 (Visual Basic では Nothing) で、フィールドがインスタンス フィールドです。
ArgumentException フィールドがオブジェクト上に存在しません。

または

value パラメータを変換し、フィールドに格納できません。

解説

このメソッドは、このインスタンスがリフレクションする、オブジェクト obj のフィールドに value を代入します。フィールドが静的な場合、 obj は無視されます。非静的フィールドの場合、 obj は、そのフィールドを継承または宣言しているクラスのインスタンスであることが必要です。新しい値はオブジェクトとして渡されます。たとえば、フィールドの型がブール型の場合は、適切なブール値を持つオブジェクトのインスタンスが渡されます。値を設定する前に、 SetValue は、必要なアクセス許可があるかどうかを確認します。この最終メソッドは、続く SetValue メソッドを呼び出すために役立ちます。

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

使用例

[Visual Basic, C#, C++] フィールドの値を設定し、その値を取得して出力し、フィールドを変更して、その変更結果を出力する例を次に示します。

 
Imports System
Imports System.Reflection
Imports System.Globalization
Imports Microsoft.VisualBasic


Public Class [MyClass]
   Private myString As String
   
   Public Sub New()
      myString = "Old value"
   End Sub 'New
   
   ReadOnly Property GetField() As String
      Get
         Return myString
      End Get
   End Property
End Class '[MyClass]


Public Class FieldInfo_SetValue
   
   Public Shared Sub Main()
      Try
         Dim myObject As New [MyClass]()
         Dim myType As Type = Type.GetType("MyClass")
         Dim myFieldInfo As FieldInfo = myType.GetField("myString", BindingFlags.NonPublic Or BindingFlags.Instance)
         ' Display the string before applying SetValue to the field.
         Console.WriteLine(ControlChars.Lf + "The field value of myString is {0}.", myFieldInfo.GetValue(myObject))
         ' Display the SetValue signature used to set the value of a field.
         Console.WriteLine("Applying SetValue(Object, Object).")
         ' Change the field value using the SetValue method. 
         myFieldInfo.SetValue(myObject, "New value")
         ' Display the string after applying SetValue to the field.
         Console.WriteLine("The field value of mystring is {0}.", myFieldInfo.GetValue(myObject))
         ' Set the field value to its old value. 
         myFieldInfo.SetValue(myObject, "Old value")
         myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic Or BindingFlags.Instance)
         ' Display the string before applying SetValue to the field.
         Console.Write(ControlChars.Lf + "The field value of mystring is {0}." + ControlChars.Lf, myFieldInfo.GetValue(myObject))
         ' Display the SetValue signature used to set the value of a field.
         Console.WriteLine("Applying SetValue(Object, Object, BindingFlags, Binder, CultureInfo).")
         ' Change the field value using the SetValue method. 
         myFieldInfo.SetValue(myObject, "New value", BindingFlags.Public, Nothing, Nothing)
         ' Display the string after applying SetValue to the field.
         Console.WriteLine("The field value of mystring is {0}.", myFieldInfo.GetValue(myObject))
      Catch e As Exception
         ' Any exception generated is displayed. 
         Console.WriteLine("Exception: {0}", e.Message)
      End Try
   End Sub 'Main
End Class 'FieldInfo_SetValue

[C#] 
using System;
using System.Reflection;
using System.Globalization;

public class MyClass
{
    private string myString;
    public MyClass()
    {
        myString = "Old value";
    }
    string GetField
    {
        get
        {
            return myString;
        }
    }
}

public class FieldInfo_SetValue
{
    public static void Main()
    {
        try
        {
            MyClass myObject = new MyClass();
            Type myType = Type.GetType("MyClass");
            FieldInfo myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance); 
            // Display the string before applying SetValue to the field.
            Console.WriteLine( "\nThe field value of myString is {0}.", myFieldInfo.GetValue(myObject)); 
            // Display the SetValue signature used to set the value of a field.
            Console.WriteLine( "Applying SetValue(Object, Object).");    
            // Change the field value using the SetValue method. 
            myFieldInfo.SetValue(myObject, "New value"); 
            // Display the string after applying SetValue to the field.
            Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
            // Set the field value to its old value. 
            myFieldInfo.SetValue( myObject , "Old value" ); 
            myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance); 
            // Display the string before applying SetValue to the field.
            Console.Write( "\nThe field value of mystring is {0}.\n", myFieldInfo.GetValue(myObject)); 
            // Display the SetValue signature used to set the value of a field.
            Console.WriteLine("Applying SetValue(Object, Object, BindingFlags, Binder, CultureInfo)."); 
            // Change the field value using the SetValue method. 
            myFieldInfo.SetValue(myObject, "New value", BindingFlags.Public, null, null);     
            // Display the string after applying SetValue to the field.
            Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
        }
        catch( Exception e )
        {
            // Any exception generated is displayed. 
            Console.WriteLine( "Exception: {0}", e.Message );
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Globalization;

public __gc class MyClass
{
private:
   String* myString;
public:
   MyClass()
   {
      myString = S"Old value";
   }
   __property String* get_GetField()
   {
      return myString;
   }
};

int main()
{
   try {
      MyClass* myObject = new MyClass();
      Type* myType = Type::GetType(S"MyClass");
      FieldInfo* myFieldInfo = myType->GetField(S"myString", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance)); 
      // Display the String* before applying SetValue to the field.
      Console::WriteLine(S"\nThe field value of myString is {0}.", myFieldInfo->GetValue(myObject)); 
      // Display the SetValue signature used to set the value of a field.
      Console::WriteLine(S"Applying SetValue(Object, Object).");    
      // Change the field value using the SetValue method. 
      myFieldInfo->SetValue(myObject, S"New value"); 
      // Display the String* after applying SetValue to the field.
      Console::WriteLine(S"The field value of mystring is {0}.", myFieldInfo->GetValue(myObject));
      // Set the field value to its old value. 
      myFieldInfo->SetValue(myObject , S"Old value"); 
      myFieldInfo = myType->GetField(S"myString", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance)); 
      // Display the String* before applying SetValue to the field.
      Console::Write(S"\nThe field value of mystring is {0}.\n", myFieldInfo->GetValue(myObject)); 
      // Display the SetValue signature used to set the value of a field.
      Console::WriteLine(S"Applying SetValue(Object, Object, BindingFlags, Binder, CultureInfo)."); 
      // Change the field value using the SetValue method. 
      myFieldInfo->SetValue(myObject, S"New value", BindingFlags::Public, 0, 0);     
      // Display the String* after applying SetValue to the field.
      Console::WriteLine(S"The field value of mystring is {0}.", myFieldInfo->GetValue(myObject));
   } catch (Exception* e) {
      // Any exception generated is displayed. 
      Console::WriteLine(S"Exception: {0}", e->Message);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: 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 セキュリティ:

参照

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