开发者

Using Reflection to set a static variable value before object's initialization?

开发者 https://www.devze.com 2022-12-18 23:51 出处:网络
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 hopin开发者_开发知识库g there\'s a way to

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 hopin开发者_开发知识库g there's a way to get around this.


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 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消