I have a static class
with static private readonly
member that\'s set via the class\'s static constructor
. Below is a simplified example.
Here is a quick example showing how to do it:
using System;
using System.Reflection;
class Example
{
static void Main()
{
var field = typeof(Foo).GetField("bar",
BindingFlags.Static |
BindingFlags.NonPublic);
// Normally the first argument to "SetValue" is the instance
// of the type but since we are mutating a static field we pass "null"
field.SetValue(null, "baz");
}
}
static class Foo
{
static readonly String bar = "bar";
}
This "null rule" also applies to FieldInfo.GetValue() for a static field, e.g.,
Console.Writeline((string)(field.GetValue(null)));