C# FieldInfo SetValue(Object, Object)
Description
FieldInfo SetValue(Object, Object)
Sets the value of
the field supported by the given object.
Syntax
FieldInfo.SetValue(Object, Object)
has the following syntax.
public void SetValue(
Object obj,
Object value
)
Parameters
FieldInfo.SetValue(Object, Object)
has the following parameters.
obj
- The object whose field value will be set.value
- The value to assign to the field.
Returns
FieldInfo.SetValue(Object, Object)
method returns
Example
using System;//from w w w . j av a2 s . com
using System.Reflection;
using System.Globalization;
public class Example
{
private string myString;
public Example()
{
myString = "Old value";
}
public string StringProperty
{
get
{
return myString;
}
}
}
public class FieldInfo_SetValue
{
public static void Main()
{
Example myObject = new Example();
Type myType = typeof(Example);
FieldInfo myFieldInfo = myType.GetField("myString",
BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine(myFieldInfo.GetValue(myObject));
myFieldInfo.SetValue(myObject, "New value");
Console.WriteLine(myFieldInfo.GetValue(myObject));
}
}
The code above generates the following result.