mercredi 3 août 2022

Unable to cast reflection called method type IEnumerable

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





Aucun commentaire:

Enregistrer un commentaire