I am trying to find via reflection the most appropriate method to invoke, when i have a type to pass to that method.
The edge case that is worrying me is overloading with same number of parameters, like so:
class UserClass {}
class UserClassB : UserClass {}
class SomeClass {
void method(object x);
void method(UserClass x);
}
in runtime i am interested in invoking method, while the type i have in hand is UserClassB.
The most appropriate would be method(UserClass).
The problem is that when using typeof(SomeClass).GetMethod("method", new Type[] { typeof(UserClass2) })); it will return a null, cause i think it searched based on exact match for the passed types.
Any ideas? Thanks.
You’re missing the binding flags.
use overload that you can pass the following flags:
BindingFlags.Instance | BindingFlags.NonPublic
you’re getting null because the GetMethod will look for public method unless you specify you want non public methods too.