Is there anyway to set the value of a static (private) variable on an object that has not been initialized? The SetValue
method requires an instance, but I'm hoping there's a way to get around this.
JaredPar
For static values you can pass null for the instance parameter.
var type = typeof(SomeClass);
var field = type.GetField("SomeField", BindingFlags.NonPublic | BindingFlags.Static);
field.SetValue(null, 42);
could you create a static function that is public and use it to set your private static variable ?
来源:https://stackoverflow.com/questions/2203545/using-reflection-to-set-a-static-variable-value-before-objects-initialization