简体   繁体   中英

Unable to cast reflection called method type IEnumerable<Tuple>

I have an error when I call a method in reflection, I can't cast the result to its base Type (IEnumerable<Tuple<SortingOrder, Expression>>) which seems strange.

I don't have this problem when I cast another method in Expression. I guess it's the IEnumerable or the Tuple that is the problem. Does anyone have any idea how to do this?

Error when I call the first method below:

public static IEnumerable<Tuple<SortingOrder, Expression>> ConvertOrderByToTupleExpression(Type entityType, string orderBy)
{
    return (IEnumerable<Tuple<SortingOrder, Expression>>)typeof(RepositoryReflectionHelper)
                .GetMethods()
                .First(x => x.Name == nameof(RepositoryReflectionHelper.ConvertOrderByToTupleExpression) && x.IsGenericMethod && x.GetParameters().Count() == 1)
                .MakeGenericMethod(new Type[] { entityType })
                .Invoke(null, new object[] { orderBy });
}

public static IEnumerable<Tuple<SortingOrder, Expression<Func<TEntity, object>>>> ConvertOrderByToTupleExpression<TEntity>(string orderBy)
{
    List<Tuple<SortingOrder, Expression<Func<TEntity, object>>>> orderByCriteriaTuple = new List<Tuple<SortingOrder, Expression<Func<TEntity, object>>>>();

    return orderByCriteriaTuple;
}

Thank for your times

C# does not support variance for classes but you can leverage the fact that it supports variance for interfaces, IEnumerable<T> is covariant interface and Tuple<T1, T2> implements ITuple :

object tuple = new[]
{
    new Tuple<int, Expression<Func<int>>>(1, () => 1)
};

IEnumerable<Tuple<int, Expression>> enumerable = ((IEnumerable<ITuple>)tuple)
    .Select(t => Tuple.Create((int)t[0], (Expression) t[1]))
    .ToList(); // to list to make sure it does work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM