I'm trying to invoke a method using reflection.
Something like this:
method.Invoke(instance, propValues.ToArray())
The problem is that there isn't a way to ensure the array of parameters is in the right order. Is there a way to specific which values goes on which parameter by name? Or do I really have to make a custom binder? If so, can anyone guide me in the right direction?
Well, you specify them in parameter order. So if you want to map specific values to specific names, you should fetch the parameter list with
method.GetParameters
and map them that way. For example, if you had aDictionary<string, object>
with the parameters:EDIT: This answer focuses on parameter types not the parameter names. If the code is obfuscated (or having different param names) then it will be difficult to map the solution that Jon Skeet has provided.
Anyway, I had been playing with this a lot.... This is what works best for me (without knowing param names) :
so, you can call the above function:
The above call will should be able to invoke this method with matching signature:
Please note that he above example is in the scope of '
this
'