Search code examples
c#.netreflectioncasting

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;
}

UPDATE:

Same Errors for "reverse" actions in the below code:

IEnumerable<Tuple<SortingOrder, Expression>> orderByExpression = RepositoryReflectionHelper.ConvertOrderByToTupleExpression(entityType, orderBy);

var calledFunction = GetManyPaging(orderByExpression);

public IEnumerable<TEntity> GetManyPaging(IEnumerable<System.Tuple<SortingOrder, Expression<Func<TEntity, object>>>> orderByCriteriaTuple)
{
    // Do something not significant
}



Thank for your times


Solution

  • 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