I believe the answer to your question is, "There is no way to do this".
Since GetMethod is not able to "MakeGenericMethod" while its doing its lookup, you have a method with one generic parameter that's known (TClass) and one that's not (TMethod). Reflection can't look up a method when some, but not all, of the parameters are known.
Note- Even though you do know what TMethod is supposed to be ("int" in your example), this would require the "MakeGenericMethod" that I referenced in the previous paragraph.
To do this you need to know the generic types, I don't think your task would be possible otherwise.
If your intention was to create a generic methodinfo for a generic class, before it knows any of its types, so you can later call
MakeGenericType with your two types on it, that won't possible.
This can be shown by calling
typeof(MyClass<>).GetMethod("MyMethod"), which will return null.
If you know the specific types on the other hand, that's easy:
static MethodInfo GetMethod(Type classType, Type methodType)
var genericClassType = typeof(MyClass<>).MakeGenericType(classType);
var methodInfo = genericClassType.GetMethod("MyMethod");
var genericMethodInfo = methodInfo.MakeGenericMethod(methodType);
Notice that I don't create a generic type for the
When you create the
genericClassType and call
GetMethod on it, the CLR doesn't know yet, what type
TMethod would be. This is only know as soon as you call
methodInfo. Therefore if you would call
GetMethod with a fully parametrized
Expression<Func<TClass,TMethod>> type, it wouldn't find the method.
That's why you need to call
genericClassType.GetMethod("MyMethod") without the parameter types and possibly have to filter it, if the method is overloaded. Afterwards you can call
MakeGenericMethod(methodType) and have your fully parametrized methodInfo object.