mercredi 13 février 2019

How to find a MethodInfo (potentially generic) from only name and specific arguments?

Given:

  • Type (that contains the method/s)
  • Name of the method
  • Type array of the arguments that

Wanted:

  • The MethodInfo, however the method can have many overloads, and even generic-type-parameters. So a classical type.GetMethod(..) will not help us at all!

This is actually not so easy to do since you do not have the generic-arguments. So you can't create a method using methodInfo.MakeGenericMethod(...) because you don't even know what types to use.

What it really needed, is a way that inspects all parameters, recursively tracks all unspecified generic-arguments, and infers the generic arguments from the given specific parameters.

I already asked this question previously, but unfortunately someone just rudely closed it as a duplicate when it was really not. There is no answer yet that actually solves this. So I'll present the solution I came up with.





Aucun commentaire:

Enregistrer un commentaire