使用反射来设置对象的初始化前的静态变量的值? [英] Using Reflection to set a static variable value before object's initialization?

查看:103
本文介绍了使用反射来设置对象的初始化前的静态变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正是有设定尚未初始化的对象上的静态(私有)变量的值?在的SetValue 方法需要一个实例,但我希望有一种方法来解决这个问题。

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.

推荐答案

有关静态值可以为实例参数传递null。

For static values you can pass null for the instance parameter.

var type = typeof(SomeClass);
var field = type.GetField("SomeField");
field.SetValue(null, 42);

这篇关于使用反射来设置对象的初始化前的静态变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆