当T在编译时未知时,将IEnumerable转换为IEnumerable <T>

Ela*_*hmi 5 c# generics

我有一个带有以下签名的排序扩展方法:

public static IEnumerable<T> CustomSort<T>(this IEnumerable<T> source, string sortProperties)
Run Code Online (Sandbox Code Playgroud)

我们写了一段时间,它一直在做它的事情.现在我正在创建一个自定义控件,DataSource属性是一个IEnumerable(非泛型).有没有办法在非通用的IEnumerable中获取对象的类型?

我确信"排序自定义控制数据源"的问题已经解决了一百万次,但我似乎无法找到解决方案.

Mar*_*ell 5

这里有一个基本问题,即一个类型可以同时为多个 T 实现 IEnumerable-of-T。但如果我们排除这种情况,一个厚脸皮的方法是:

void Evil<T>(IEnumerable<T> data) {...}

IEnumerable source = ...
dynamic cheeky = source;
Evil(cheeky);
Run Code Online (Sandbox Code Playgroud)

这基本上将这个问题转移到 DLR,让你的 Evil-of-T 方法轻松解决。


arm*_*oon 1

您可以创建一个扩展方法,该方法将在运行时返回正确的类型:

public static class LinqExtensions
{
    public static Type GetElementType(this IEnumerable source)
    {
        var enumerableType = source.GetType();
        if (enumerableType.IsArray)
        {
            return enumerableType.GetElementType();
        }
        if (enumerableType.IsGenericType)
        {
            return enumerableType.GetGenericArguments().First();
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:我添加了用于对IEnumerable<T>非通用执行通用特定排序的机制IEnumerable

public static class SortingExtensions
{
    public static IEnumerable<T> CustomSort<T>(this IEnumerable<T> source, string sortProperties)
    {
        // sort here
    }

    public static IEnumerable CustomSort(this IEnumerable source, string sortProperties)
    {
        var elementType = source.GetElementType();
        var genericElementType = typeof (IEnumerable<>).MakeGenericType(elementType);

        var sortMethod = typeof (SortingExtensions).GetMethod(
            "CustomSort", 
            BindingFlags.Public | BindingFlags.Static,
            null, 
            new [] {genericElementType, typeof (string)},
            null);

        return (IEnumerable) sortMethod.Invoke(null, new object[] {source, sortProperties});
    }

}
Run Code Online (Sandbox Code Playgroud)