在C#中,如何从通用定义和通用参数(如
var genericDefinition = typeof(List);
var genericArgument = typeof(string);
// How can I get the Type instance representing List<string> from the 2 variables above?
在我的用例中,一般参数是动态解析的。这在C#中可能吗?提前致谢。
没有这样的东西typeof(List)
。但是,typeof(List<>)
工作正常,并且是开放的泛型类型。然后,您只需使用:
var genericDefinition = typeof(List<>);
var genericArgument = typeof(string);
var concreteListType = genericDefinition.MakeGenericType(new[] {genericArgument});
并且您应该找到的concreteListType
是typeof(List<string>)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句