确定集合是否类型为IEnumerable< T>

编程入门 行业动态 更新时间:2024-10-13 18:26:05
本文介绍了确定集合是否类型为IEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何确定对象是否类型为IEnumerable< T>?

代码:

namespace NS { class Program { static IEnumerable< int> GetInts(){ yield return 1; } static void Main(){ var i = GetInts(); var type = i.GetType(); Console.WriteLine(type.ToString()); } } }

输出:

NS.1.Program +< GetInts> d__0

b $ b

如果我改变GetInts返回IList,一切正常输出是:

System.Collections.Generic.List`1 [System.Int32]

命名空间NS { class Program { static IEnumerable< int& GetInts(){ yield return 1; } static void Main(){ var i = GetInts(); var type = i.GetType(); Console.WriteLine(type.Equals(typeof(IEnumerable< int>)))); } } }

解决方案>

如果您的意思是集合,则为:

var asEnumerable = i as IEnumerable< int> ;; if(asEnumerable!= null){...}

假设(从示例中)您拥有类型:

永远不是of类型 IEnumerable< int> - 但它可能会实现我希望:

if(typeof(IEnumerable< int>)。IsAssignableFrom(type)){...}

如果你不知道上面的 T ( int ),那么检查所有实现的接口: p>

static类型GetEnumerableType(类型类型){ foreach(type.GetInterfaces()中的类型intType){ if(intType.IsGenericType & intType.GetGenericTypeDefinition()== typeof(IEnumerable<>)){ return intType.GetGenericArguments()[0]; } } return null; }

并致电:

类型t​​ = GetEnumerableType(type);

如果为null,则不是 IEnumerable< T& code>任何 T - 否则请检查 t 。

How to determine if object is of type IEnumerable <T>?

Code:

namespace NS { class Program { static IEnumerable<int> GetInts() { yield return 1; } static void Main() { var i = GetInts(); var type = i.GetType(); Console.WriteLine(type.ToString()); } } }

Output:

NS.1.Program+<GetInts>d__0

If I change GetInts to return IList, everything is OK the output is:

System.Collections.Generic.List`1[System.Int32]

And this returns false:

namespace NS { class Program { static IEnumerable<int> GetInts() { yield return 1; } static void Main() { var i = GetInts(); var type = i.GetType(); Console.WriteLine(type.Equals(typeof(IEnumerable<int>))); } } }

解决方案

If you mean the collection, then just as:

var asEnumerable = i as IEnumerable<int>; if(asEnumerable != null) { ... }

However, I'm assuming (from the example) that you have a Type:

The object will never be "of" type IEnumerable<int> - but it might implement it; I would expect that:

if(typeof(IEnumerable<int>).IsAssignableFrom(type)) {...}

would do. If you don't know the T (int in the above), then check all the implemented interfaces:

static Type GetEnumerableType(Type type) { foreach (Type intType in type.GetInterfaces()) { if (intType.IsGenericType && intType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) { return intType.GetGenericArguments()[0]; } } return null; }

and call:

Type t = GetEnumerableType(type);

if this is null, it isn't IEnumerable<T> for any T - otherwise check t.

更多推荐

确定集合是否类型为IEnumerable&lt; T&gt;

本文发布于:2023-11-09 04:24:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571396.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   IEnumerable   amp   lt   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!