can I specify an Type wildcard for GetMethod?
I have both methods:
I can invoke
DoFoo
with two parameters through reflection with:
However, I'm in a scenario where I don't know what one of the types is, because I get them from a collection of objects and through each object I get its type. When an object is null, it is not possible to determine its type, as they are all object?[]
.
I needed a way to specify a wildcard in a certain type position. Is there any way to do this?3 Replies
this snippet:
the compiler automatically select
Foo(string, string?, string?)
as the method for Foo
, even if I've not specified any argument but null
. How did the compiler made this decision?string is more specific than object, because string inherits from object
therefore the string overload is better
but no, you essentially have to use GetMethods and then replicate a piece of the compiler's overload resolution logic
u could use Type.GetMethods(BindingFlags) to get an array of MethodInfo on which u can filter, and use its
GetParameters()
method to get an array of ParameterInfo to analyze it in a more complex fashion
(sorry forgot to scroll down :s)