Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
无论如何要在尚未初始化的对象上设置静态(私有)变量的值?该SetValue方法需要一个实例,但我希望有办法解决这个问题。
SetValue
对于静态值,您可以为实例参数传递 null。
var type = typeof(SomeClass); var field = type.GetField("SomeField", BindingFlags.NonPublic | BindingFlags.Static); field.SetValue(null, 42);
你能创建一个公共的静态函数并用它来设置你的私有静态变量吗?