有了这个代码:
World w = new World();
var data = GetData<World>(w);
如果我有w
带有反射,这可以是类型World
, Ambient
, Domention
, etc.
我怎样才能得到GetData
???
我只有实例对象:
var data = GetData<???>(w);
var type = <The type where GetData method is defined>;
var genericType = typeof(w);
var methodInfo = type.GetMethod("GetData");
var genericMethodInfo = methodInfo.MakeGenericMethod(genericType);
//instance or null : if the class where GetData is defined is static, you can put null : else you need an instance of this class.
var data = genericMethodInfo.Invoke(<instance or null>, new[]{w});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)