Click here to Skip to main content
15,881,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I was just wondering if it would be possible to invoke a method via reflection if I have a set of
C#
KeyValuePair<string, object>
that describe the parameters and their values?

Point is that it seems that I might not always have the position of the parameter in the methods' signature, so it is impossible to create the parameter array for the method in the correct order.

Is it possible to invoke the method using parameter names?
Any help is kindly appreciated,

cheers and best regards
Andy
Posted
Updated 5-Jun-12 21:24pm
v2

It is possible to Invoke a method using named parameters as explained here http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx[^]

In one of the over loads of the Type.InvokeMember method we can pass an array of namedParameters as explained here http://msdn.microsoft.com/en-us/library/de3dhzwy[^]

The method of a class can be invoked using parameter names and values stored in a List of KeyValuePairs as shown below:
C#
Car car = new Car();
List<KeyValuePair<string,object>> paramData = new List<KeyValuePair<string,object>>();
//The order of parameters is given as parameter2, parameter1
paramData.Add(new KeyValuePair<string,object>("param2",50));
paramData.Add(new KeyValuePair<string,object>("param1","speed"));

car.GetType().InvokeMember("PrintData", BindingFlags.InvokeMethod, null, car,
    paramData.Select (d => d.Value).ToArray()
    ,null,null, paramData.Select (d => d.Key).ToArray());
//Output
//param1=speed and param2 = 50

List<KeyValuePair<string,object>> paramData2 = new List<KeyValuePair<string,object>>();
//The order of parameters is changed as parameter1, parameter2
paramData2.Add(new KeyValuePair<string,object>("param1","Speed---"));
paramData2.Add(new KeyValuePair<string,object>("param2",100));

car.GetType().InvokeMember("PrintData", BindingFlags.InvokeMethod, null, car,
    paramData2.Select (d => d.Value).ToArray()
    ,null,null, paramData2.Select (d => d.Key).ToArray());
//Output
//param1=Speed--- and param2 = 100

public class Car {
    public void PrintData(string param1, int param2){
        Console.WriteLine ("param1={0} and param2 = {1}",param1,param2);
    }
}
 
Share this answer
 
v3
Comments
hoernchenmeister 6-Jun-12 4:53am    
Perfect VJ Reddy, just what I was looking for.
Thanks a ton and have a great day
Andy
VJ Reddy 6-Jun-12 5:02am    
You're welcome and thank you for accepting the solution :)
Sandeep Mewara 6-Jun-12 5:16am    
Good answer. 5!
VJ Reddy 6-Jun-12 5:27am    
Thank you, Sandeep :)
Maciej Los 6-Jun-12 18:00pm    
Good work, my 5!
Yes this is possible. You will have to use the more elaborate method Type.InvokeMember[^].
The page describing the BindingFlags[^] parameter of the Type.InvokeMember method has a quite elaborate example with different usage scenarios. Search the page for this string: Invoking a method with named parameters which will take you to this part of the code:
C#
Console.WriteLine();
Console.WriteLine("Invoking a method with named parameters.");
Console.WriteLine("---------------------------------------");
// BindingFlags.InvokeMethod
// Call a method using named parameters.
object[] argValues = new object [] {"Mouse", "Micky"};
String [] argNames = new String [] {"lastName", "firstName"};
t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);


Regards,

Manfred
 
Share this answer
 
Comments
hoernchenmeister 6-Jun-12 5:47am    
Thanks a lot for your answer Manfred.
It is very much appreciated.
have agreat day and best regards
Andy
Manfred Rudolf Bihy 6-Jun-12 7:52am    
You're welcome!
VJ Reddy 6-Jun-12 5:53am    
Good answer. 5!
Manfred Rudolf Bihy 6-Jun-12 7:52am    
Thanks! :)
Maciej Los 6-Jun-12 18:00pm    
Good answer, my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900