栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从IEnumerable获取类型T

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从IEnumerable获取类型T

IEnumerable<T> myEnumerable;Type type = myEnumerable.GetType().GetGenericArguments()[0];

因此,

IEnumerable<string> strings = new List<string>();Console.WriteLine(strings.GetType().GetGenericArguments()[0]);

版画

System.String

请参阅MSDN的

Type.GetGenericArguments

编辑: 我相信这将解决评论中的关注点:

// returns an enumeration of T where o : IEnumerable<T>public IEnumerable<Type> GetGenericIEnumerables(object o) {    return o.GetType() .GetInterfaces() .Where(t => t.IsGenericType     && t.GetGenericTypeDefinition() == typeof(IEnumerable<>)) .Select(t => t.GetGenericArguments()[0]);}

一些对象实现了多个泛型,

IEnumerable
因此有必要返回它们的枚举。

编辑: 虽然,我不得不说,对于一个类实现

IEnumerable<T>
多个对象是一个糟糕的主意
T



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号