将IEnumerable转换为IEnumerable< T>当T直到运行时才知道 [英] Convert IEnumerable to IEnumerable<T> when T isn't known until runtime

查看:141
本文介绍了将IEnumerable转换为IEnumerable< T>当T直到运行时才知道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当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 to IEnumerable<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 and T 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 of T 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 type IEnumerable<T> (where T is the type represented by the elementType variable). But keep in mind that statically, genericResults is still of type object; there is no way to tell the compiler that it is an IEnumerable<T>, since T isn't known statically.

这篇关于将IEnumerable转换为IEnumerable&lt; T&gt;当T直到运行时才知道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆