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.
Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call non-static methods. However, non-static methods have access to all variables (instance or static) and methods (static or non-static) in the class.
A class or struct can only have one static constructor. Static constructors cannot be inherited or overloaded. A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With