Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FieldInfo.SetValue Method

Sets the value of the field for the given object to the given value.

Overload List

Sets the value of the field supported by the given object.

Supported by the .NET Compact Framework.

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

When overridden in a derived class, sets the value of the field supported by the given object.

Supported by the .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);

Example

[Visual Basic, C#, C++] The following example sets the value of a field, gets and displays the value, modifies the field, and displays the result.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SetValue. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

FieldInfo Class | FieldInfo Members | System.Reflection Namespace

Show:
© 2014 Microsoft