将IEnumerable转换为IEnumerable< T>当T直到运行时才知道 [英] Convert IEnumerable to IEnumerable<T> when T isn't known until runtime
问题描述
当T直到运行时才知道T时,如何将 IEnumerable
转换为 IEnumerable< T>
p>
我有一个方法,它绝对需要一个 IEnumerable< T>
/ strong>和 T
必须是实际类型,而不是类似对象的祖先类型
我打赌反射API允许这样的事情,但我不知道具体如何。
更新
我可以得到这样的类型对象:
var first = results.Cast< object>()。 FirstOrDefault();
变量指向键入
if(first!= null)
{
var type = first.GetType();
$ h2 Update2
我所指的方法实际上是WPF DataGrid的ItemSource属性的Setter。
我遇到的问题是,如果我没有给它传递IEnumerable< T>
,Datagrid中生成的行都是空白的,就好像它是反映T
属性的方法不起作用,它不能生成列。
即使使用这个代码有这种不必要的效果:
public CollectionResultWindow(IEnumerable结果)
{
Contract.Requires(results != null,结果为空。
InitializeComponent();
DataContext = this;
var first = results.Cast< object>()。FirstOrDefault();
if(first!= null)
{
Type type = first.GetType();
var castMethod = typeof(Enumerable).GetMethod(Cast)。MakeGenericMethod(type);
dynamic genericResults = castMethod.Invoke(null,new [] {results});
结果= genericResults; //结果是DataGrid.ItemSource绑定的依赖项属性。
}
}
解决方案
var elementType = ...
var results = ...
var castMethod = typeof(Enumerable).GetMethod(Cast)。MakeGenericMethod(elementType );
var genericResults = castMethod.Invoke(null,new [] {results});
genericResults IEnumerable< T>
(其中T
是由elementType
变量)。但请记住,静态地,genericResults
仍然是类型object
;没有办法告诉编译器它是IEnumerable< T>
,因为T
不是静态的。How can I convert
IEnumerable
toIEnumerable<T>
when T isn't known until runtime?I have a method that absolutely needs an
IEnumerable<T>
where T isn't known until runtime andT
must be the actual type and not an ancestor type like object.I'm betting the reflection API allows for something like this but I don't know exactly how.
Update
I can get a type object like this:
var first = results.Cast<object>().FirstOrDefault(); if (first != null) { var type = first.GetType(); }
Update2
The "method" I was referring to was actually the Setter of the WPF DataGrid's ItemSource property. The problem I'm having is that if I don't pass it an
IEnumerable<T>
the rows generated in the Datagrid are all blank, as if it's method of reflecting over the properties ofT
is not working and it can't generate columns.Even using this code has this unwanted effect:
public CollectionResultWindow(IEnumerable results) { Contract.Requires(results != null, "results is null."); InitializeComponent(); DataContext = this; var first = results.Cast<object>().FirstOrDefault(); if (first != null) { Type type = first.GetType(); var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(type); dynamic genericResults = castMethod.Invoke(null, new[] { results }); Results = genericResults; // Results is a dependency property that DataGrid.ItemSource is bound to. } }
解决方案var elementType = ... var results = ... var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(elementType); var genericResults = castMethod.Invoke(null, new[] { results });
The
genericResults
variable refers to an object of typeIEnumerable<T>
(whereT
is the type represented by theelementType
variable). But keep in mind that statically,genericResults
is still of typeobject
; there is no way to tell the compiler that it is anIEnumerable<T>
, sinceT
isn't known statically.这篇关于将IEnumerable转换为IEnumerable< T>当T直到运行时才知道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!