lundi 13 novembre 2017

Using reflection to distinguish generic methods with generic arguments

In EntityFrameworkQueryableExtensions there are two methods, both called ThenInclude, with the following signatures:

public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(this IIncludableQueryable<TEntity, TPreviousProperty> source, Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath)where TEntity : class

and

public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(this IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>> source, Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath)where TEntity : class

The difference is that the second signature has IEnumerable<TPreviousProperty> in the type of the 'this' argument, while the first signature has just TPreviousProperty.

The question is, how can I get one the MethodInfo for the second one (or the first one for that matter) using reflection and MakeGenericMethod?

So far all I've been able to come up with is to add an extra layer like this:

class Whatever<TEntity> where TEntity: class {
    private static MethodInfo ThenIncludeEnumerableMethod<TPreviousProperty,TProperty>()
    {
        Func<IIncludableQueryable<TEntity,IEnumerable<TPreviousProperty>>, Expression<Func<TPreviousProperty, TProperty>>, IIncludableQueryable<TEntity, TProperty>> thenIncludeLambda = (source, lambda) => source.ThenInclude(lambda);
        return thenIncludeLambda.Method;
    }
}

There should be a more direct way to do it.

Note Reflection: How to get a generic method? is not an answer to this question.





Aucun commentaire:

Enregistrer un commentaire