How can I find a generic overloaded method? For example, Queryable‘s
public static IQueryable<TResult> Select<TSource , TResult> ( this IQueryable<TSource> source , Expression<Func<TSource , int , TResult>> selector );
I’ve looked for existing solutions, and they’re either not generic enough (are based on the method’s parameters count, etc.), need more parameters than I have (require generic type definitions or arguments), or just plain wrong (don’t account for nested generics, etc.)
I have the defining class type — Type type, the method name — string name and the array of the parameter types (not the generic definitions) — Type[] types.
So far it seems I have to map each of prospective method’s .GetGenericArguments() to a specific type by comparing the (generic type tree?) of the method’s .GetParameters ().Select (p=>p.ParameterType) with the corresponding item in the types array, thus deducing the generic arguments of the method, so I can .MakeGenericMethod it.
This seems a bit too complicated for the task, so maybe I’m overthinking the whole thing.
Any help?
Ok, so I just coded this, which is essentially a manual type inference, and I think should do what I need.
So given
I can pick the method I want:
This seem to support non-generic methods, but doesn’t support explicit generic arguments (obviously), and maybe needs some work, but that’s the core of it.