C# 如何从IEnumerable中提取类型对象<;T>;

C# 如何从IEnumerable中提取类型对象<;T>;,c#,reflection,ienumerable,system.reflection,C#,Reflection,Ienumerable,System.reflection,我对以下函数有一个定义 void Func<T>(IEnumerable<T> e) { // Need type of T here Type t = e.CollectionElementsType() ??? } void函数(IEnumerable e) { //这里需要T型 类型t=e.CollectionElementsType()??? } 现在让它更复杂,我有以下代码 void Func(IEnumerable<object>

我对以下函数有一个定义

void Func<T>(IEnumerable<T> e)
{
   // Need type of T here
   Type t = e.CollectionElementsType() ???
}
void函数(IEnumerable e)
{
//这里需要T型
类型t=e.CollectionElementsType()???
}
现在让它更复杂,我有以下代码

void Func(IEnumerable<object> e)
{
   // Need type of T here
   Type t = e.CollectionElementsType() ???
}
void函数(IEnumerable e)
{
//这里需要T型
类型t=e.CollectionElementsType()???
}
现在假设我对第二个实现有以下调用

Func(new List<int>().Cast<object>());
Func(新列表().Cast());
我希望Func检测int类型。 我如何才能做到这一点?

为什么不这样做

Type t = typeof(T);
阅读更多关于

编辑:

对于更复杂的情况,可以有多个类型,因此可以执行以下操作:

IEnumerable<Type> GetTypes (IEnumerable<object> list)
{
    foreach (object a in list)
    {
       yield return a.GetType();
    }
}
IEnumerable获取类型(IEnumerable列表)
{
foreach(列表中的对象a)
{
返回a.GetType();
}
}
如果它实际上只有一种类型,那么可以编写
typet=GetTypes(您的可枚举项).FirstOrDefault()
GetType()
将获取对象的真实类型

阅读

为什么不这样做

Type t = typeof(T);
阅读更多关于

编辑:

对于更复杂的情况,可以有多个类型,因此可以执行以下操作:

IEnumerable<Type> GetTypes (IEnumerable<object> list)
{
    foreach (object a in list)
    {
       yield return a.GetType();
    }
}
IEnumerable获取类型(IEnumerable列表)
{
foreach(列表中的对象a)
{
返回a.GetType();
}
}
如果它实际上只有一种类型,那么可以编写
typet=GetTypes(您的可枚举项).FirstOrDefault()
GetType()
将获取对象的真实类型

阅读关于

//返回T的枚举,其中o:IEnumerable
公共IEnumerable GetGenericEnumerables(对象o){
返回o.GetType()
.GetInterfaces()
。其中(t=>t.IsGenericType==true
&&t.GetGenericTypeDefinition()==typeof(IEnumerable))
.Select(t=>t.GetGenericArguments()[0]);
}
//返回T的枚举,其中o:IEnumerable
公共IEnumerable GetGenericEnumerables(对象o){
返回o.GetType()
.GetInterfaces()
。其中(t=>t.IsGenericType==true
&&t.GetGenericTypeDefinition()==typeof(IEnumerable))
.Select(t=>t.GetGenericArguments()[0]);
}

在问题中的情况下:
typeof(T)
将起作用


更一般地,可以在
e
上使用反射来提取
IEnumerable
的类型参数-
T

在问题中的情况下:
typeof(T)
将起作用


更一般地说,可以在
e
上使用反射来提取
IEnumerable
的类型参数-
T

要获取T的
类型,请使用以下方法:

void Func(IEnumerable<T> e)
{
    Type t = typeof(T);
}

要获取T的
类型
,请使用以下命令:

void Func(IEnumerable<T> e)
{
    Type t = typeof(T);
}


即使e中没有元素,第二种方法是否有效?@fahadash是的(两种方法都有效),因为它们不依赖于元素,而是依赖于
e
本身的类型。编辑问题。第二种方法在IEnumerable的情况下有效吗?@fahadash请看我对问题的评论。(另外:最好先陈述整个问题,如果你只是以递增的方式揭示真实问题,Q&A格式就不起作用。)第二种方法是否有效,即使e中没有元素?@fahadash是(两种方法都有效)因为它们不取决于元素,而是取决于
e
本身的类型。第二种方法对IEnumerable有效吗?@fahadash请看我对Q的评论。(另外:最好先陈述整个问题,如果你只是以增量方式揭示真实问题,Q&A格式就不起作用。)你想要列表中的泛型列表类型还是具体元素的类型?如果最后一个你不应该要求类型而是类型。第二个代码snipet没有意义:
t
System.Object
:你真的是说“如何确定
IEnumerable
中对象的类型?”那么答案是你必须依次提取每个元素并查看它(每个元素可能是不同的类型)@Richard由于协方差
e
可能实际上是一个
IEnumerable
,或任何其他类型的序列。您的代码将无法编译。无法将
列表
转换为
IEnumerable
。发表一些有意义的文章code@Servy:当然是这样。。。但假设这只是一个维护问题(即,采用非泛型的
IEnumerable
或make
Func
generic)。您想要泛型列表类型还是列表中具体元素的类型?如果最后一个你不应该要求类型而是类型。第二个代码snipet没有意义:
t
System.Object
:你真的是说“如何确定
IEnumerable
中对象的类型?”那么答案是你必须依次提取每个元素并查看它(每个元素可能是不同的类型)@Richard由于协方差
e
可能实际上是一个
IEnumerable
,或任何其他类型的序列。您的代码将无法编译。无法将
列表
转换为
IEnumerable
。发表一些有意义的文章code@Servy:当然是这样。。。但假设这只是一个维护问题(即,使用非通用的
IEnumerable
或使
Func
通用);可能没有。
IEnumerable
没有Count属性。底层的具体类型可能有,但您需要某种形式的cast来使用它。但是,并非所有的
IEnumerable
实现都会有这样的成员。如果多次迭代序列,它可能没有相同的项,甚至可能没有相同数量的项。这在无限序列上也会中断,在很长的序列上效率很低;可能没有。
IEnumerable
没有Count属性。底层的具体类型可能有,但您需要某种形式的cast来使用它。但是并非所有的
IEnumerable