使用反射获取泛型实例泛型类型 [英] Get generic instance generic type using reflection

查看:121
本文介绍了使用反射获取泛型实例泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设:

 键入T = typeof运算(名单<串GT;); 



要求:

 的typeof(名单<>)== SomeFunction(T)

很多时候我反映了一个类型,并希望找到返回某种类型的列表中的所有属性。我需要SomeFunction如上图所示。我找啊找,但无法弄清楚如何获得列表<> 列表<串> 。我可以使用T.GetGenericArguments(),但只返回字符串这样就不会在所有帮助。


解决方案

 的typeof(名单<串>)GetGenericTypeDefinition()== typeof运算(列表<>)


Given:

Type T = typeof(List<string>);

Requirement:

typeof(List<>) == SomeFunction(T)

Many times when I'm reflecting over a type and want to find all properties that return lists of some type...I need the "SomeFunction" shown above. I've searched and searched, but cannot figure out how to get List<> from List<string>. I can use T.GetGenericArguments(), but that only returns string so that doesn't help at all.

解决方案

typeof(List<string>).GetGenericTypeDefinition() == typeof(List<>)

这篇关于使用反射获取泛型实例泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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