FieldInfo.SetValue メソッド
指定したオブジェクトのフィールドに、指定した値を設定します。
オーバーロードの一覧
指定したオブジェクトでサポートされているフィールドの値を設定します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Sub SetValue(Object, Object)
派生クラスによってオーバーライドされた場合、指定したオブジェクトでサポートされているフィールドの値を設定します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public MustOverride Sub SetValue(Object, Object, BindingFlags, Binder, CultureInfo)
[C#] public abstract void SetValue(object, object, BindingFlags, Binder, CultureInfo);
[C++] public: virtual void SetValue(Object*, Object*, BindingFlags, Binder*, CultureInfo*) = 0;
[JScript] public abstract function SetValue(Object, Object, BindingFlags, Binder, CultureInfo);
使用例
[Visual Basic, C#, C++] フィールドの値を設定し、その値を取得して出力し、フィールドを変更して、その変更結果を出力する例を次に示します。
[Visual Basic, C#, C++] メモ ここでは、SetValue のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。