6

我有一个BindingSourcewho DataSource(定义为对象)IEnumerable在运行时可以是任何类型的类(例如IList<Foo>)。我需要将它转换为一个IQueryable<T>,以便我可以将它传递给一个通用扩展:

IOrderedQueryable<TEntity> OrderUsingSortExpression<TEntity>(this IQueryable<TEntity> source, string sortExpression) where TEntity : class

到目前为止,我有这个:

string order = "Message ASC";
Type thetype = bsTotalBindingSource.DataSource.GetType().GetGenericArguments()[0];
IEnumerable<object> totalDataSource = ((IEnumerable<object>)(bsTotalBindingSource.DataSource));
//Blowing up on this next line with 'System.Linq.Queryable is not a GenericTypeDefinition. MakeGenericType may only be called on a type for which Type.IsGenericTypeDefinition is true.'
MethodInfo asQueryableMethod = typeof(Queryable).MakeGenericType(thetype).GetMethod("AsQueryable", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(IQueryable<>) }, null); 
MethodInfo genericAsQueryableMethod = asQueryableMethod.MakeGenericMethod(thetype);
MethodInfo orderUsingSortExpressionMethod = GetType().GetMethod("OrderUsingSortExpression");
MethodInfo genericUsingSortExpressionMethod = orderUsingSortExpressionMethod.MakeGenericMethod(thetype);
bsTotalBindingSource.DataSource = genericUsingSortExpressionMethod.Invoke(this, new object[] { genericAsQueryableMethod.Invoke(totalDataSource, null), order });

正如你所看到的,这里的最终目标是能够从 DataSource 中获取一些东西,获取它的 RuntimeType IEnumerable<T>whereT可以是任何东西,然后调用AsQueryable<T>它以便可以将它传递给一个接受IQueryable<T>.

编辑在四处挖掘以找到我正在寻找的方法之后,我对这个问题有了更深入的了解。现在看起来像这样:

string order = "Message ASC";
Type thetype = bsTotalBindingSource.DataSource.GetType().GetGenericArguments()[0];
//Returns the AsQueryable<> method I am looking for
MethodInfo asQueryableMethod = typeof(Queryable).MakeGenericType(thetype).GetMethods()[1]; 
MethodInfo genericAsQueryableMethod = asQueryableMethod.MakeGenericMethod(thetype);
MethodInfo orderUsingSortExpressionMethod = typeof(SortExtension)GetType().GetMethods()[0];
MethodInfo genericUsingSortExpressionMethod = orderUsingSortExpressionMethod.MakeGenericMethod(thetype);
bsTotalBindingSource.DataSource = genericUsingSortExpressionMethod.Invoke(this, new object[] { genericAsQueryableMethod
//blows up here with 'Object of type 'System.RuntimeType' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[LogRecordDTO]'.'
.Invoke(bsTotalBindingSource.DataSource, new object[] {thetype}), order });
4

3 回答 3

6

你说你想转换IEnumerable<T>IQueryable<T>T 的类型未知的地方。

private void Test<T>(IEnumerable<T> x)
{
    var queryableX = x.AsQueryable();
}

注意:您需要是using System.Linq.

于 2012-08-24T13:10:00.050 回答
1

我认为最简单的方法是将 IEnumerable 作为参数重载(或添加扩展方法)到您要调用的方法。然后只需从中调用另一个方法。这是我的意思的一个例子。

public class MyTestClass
{
    public void Run()
    {
        List<string> myList = new List<string>() { "one", "two" };

        object myListObject = (object)myList;

        Test(myListObject);
    }

    private void Test(object myListObject)
    {
        Type myGenericType = myListObject.GetType().GetGenericArguments().First();

        MethodInfo methodToCall = typeof(MyTestClass).GetMethods().Single(
            method => method.Name.Equals("GenericMethod") && method.GetParameters().First().Name.Equals("myEnumerableArgument"));

        MethodInfo genericMethod = methodToCall.MakeGenericMethod(myGenericType);

        genericMethod.Invoke(this, new object[] { myListObject });
    }

    public void GenericMethod<T>(IQueryable<T> myQueryableArgument)
    {
    }

    public void GenericMethod<T>(IEnumerable<T> myEnumerableArgument)
    {
        GenericMethod<T>(myEnumerableArgument.AsQueryable());
    }
}
于 2012-08-24T16:02:15.810 回答
0

我认为以下可能有效:

// Get generic type argument for everything
Type theType = bsTotalBindingSource.DataSource
    .GetType().GetGenericArguments()[0];

// Convert your IEnumerable<?>-in-an-object to IQueryable<?>-in-an-object
var asQueryable =
    // Get IEnumerable<?> type
    typeof(IEnumerable<>)
    .MakeGenericType(new[] { theType })
    // Get IEnumerable<?>.AsQueryable<?> method
    .GetMethod(
        "AsQueryable",
        new Type[] { theType },
        System.Reflection.BindingFlags.Static |
        System.Reflection.BindingFlags.Public)
    // Call on the input object
    .Invoke(bsTotalBindingSource.DataSource, new object[0]);

// Sort this queryable
var asSortedQueryable =
    // Get the YourType<?> generic class your sorting generic method is in?
    // If your class isn't generic I guess you can skip the MakeGenericType.
    typeof(yourtype)
    .MakeGenericType(new[] { theType })
    // Get the OrderUsingSortExpression<?> method
    .GetMethod(
        "OrderUsingSortExpression",
        new Type[] { theType },
        System.Reflection.BindingFlags.Static |
        System.Reflection.BindingFlags.Public)
    // Call on the IQueryable<?> object
    .Invoke(dataSource, new object[] { order });

// Set this object to the data source
bsTotalBindingSource.DataSource = asSortedQueryable;

作为免责声明,我对此并不十分清楚。例如,当从 获取AsQueryable<T>IEnumerable<T>,您是否需要传入通用参数?但我确实相信你想做的事情是可能的,并且确实需要反思,我认为这大致是正确的。

于 2012-08-24T13:20:40.403 回答