C# 确定集合是否属于 IEnumerable<T> 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1846671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if collection is of type IEnumerable<T>
提问by Valentin Vasilyev
How to determine if object is of type IEnumerable <T>?
如何确定对象是否属于 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:
如果我更改 GetInts 以返回 IList,则一切正常,输出为:
System.Collections.Generic.List`1[System.Int32]
And this returns false:
这将返回 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>)));
}
}
}
采纳答案by Marc Gravell
If you mean the collection, then just as
:
如果您的意思是集合,那么只需as
:
var asEnumerable = i as IEnumerable<int>;
if(asEnumerable != null) { ... }
However, I'm assuming (from the example) that you have a Type
:
但是,我假设(从示例中)您有一个Type
:
The objectwill never be "of" type IEnumerable<int>
- but it might implementit; I would expect that:
该对象绝不会“的”类型IEnumerable<int>
-但它可能实现它; 我希望:
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:
会做。如果你不知道T
(int
在上面),那么检查所有实现的接口:
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<T>
任何T
- 否则检查t
。
回答by Heinzi
i
is of type NS.1.Program+<GetInts>d__0
, which is a subtypeof IEnumerable<int>
. Thus, you can use either
i
是类型的NS.1.Program+<GetInts>d__0
,这是一个亚型的IEnumerable<int>
。因此,您可以使用
if (i is IEnumerable<int>) { ... }
or IsAssignableFrom
(like in Marc's answer).
或IsAssignableFrom
(如马克的回答)。
回答by SwDevMan81
How to detect if type is another generic typeshould help you out with this.
如何检测类型是否是另一种泛型类型应该可以帮助您解决这个问题。
回答by tster
You can use the is
keyword.
您可以使用is
关键字。
[TestFixture]
class Program
{
static IEnumerable<int> GetInts()
{
yield return 1;
}
[Test]
static void Maasd()
{
var i = GetInts();
Assert.IsTrue(i is IEnumerable<int>);
}
}
回答by Mark Rendle
Same technique as Marc's answer, but Linqier:
与 Marc 的回答相同的技巧,但 Linqier:
namespace NS
{
class Program
{
static IEnumerable<int> GetInts()
{
yield return 1;
}
static void Main()
{
var i = GetInts();
var type = i.GetType();
var isEnumerableOfT = type.GetInterfaces()
.Any(ti => ti.IsGenericType
&& ti.GetGenericTypeDefinition() == typeof(IEnumerable<>));
Console.WriteLine(isEnumerableOfT);
}
}
}
回答by Piotr Justyna
How to determine if object is of type IEnumerable <T>?
如何确定对象是否属于 IEnumerable <T> 类型?
Please feel free to use this fine, ultra small, generic extension method to determine if any object implements IEnumerable interface. It extends the Objecttype, so you can execute it using any instance of any object you're using.
请随意使用这种精细、超小、通用的扩展方法来确定是否有任何对象实现了 IEnumerable 接口。它扩展了Object类型,因此您可以使用您正在使用的任何对象的任何实例来执行它。
public static class CollectionTestClass
{
public static Boolean IsEnumerable<T>(this Object testedObject)
{
return (testedObject is IEnumerable<T>);
}
}
回答by Serge Intern
Since IEnumerable<T> inherit IEnumerable (non generic) and if you don't need to know when a type is just IEnumerable and not IEnumerable<T> then you can use:
由于 IEnumerable<T> 继承 IEnumerable(非泛型),如果您不需要知道什么时候类型只是 IEnumerable 而不是 IEnumerable<T> 那么您可以使用:
if (typeof(IEnumerable).IsAssignableFrom(srcType))
回答by Pham Bien
Try
尝试
type.GetInterface("IEnumerable") != null && type.IsGenericType
type.GetInterface("IEnumerable") != null && type.IsGenericType
回答by Jonathan Tiefer
I believe the best way to solve this should be simple.. You first write code that returns the generic arguments (or argument in your case) of the IEnumerable<T>
then write another line to make a comparison of the argument type you desire to compare to the type contained in the IEnumerable<T>
object (int
, in your case). I have an example that displays two simple functions that handles both tasks.
我相信解决这个问题的最好方法应该很简单..你首先编写返回通用参数(或你的情况下的参数)的代码,IEnumerable<T>
然后写另一行来比较你想要比较的参数类型与类型包含在IEnumerable<T>
对象中(int
,在您的情况下)。我有一个示例,它显示了处理这两个任务的两个简单函数。
public static Type GetEnumerableArgumentType(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
return type.GetGenericArguments()[0];
else
return null;
}
public static bool EnumerableContainsArgumentType(Type tEnumerable, Type tGenArg)
{
return GetEnumerableArgumentType(tEnumerable) == tGenArg;
}