通过使用参数名称的反射来调用方法? [英] Invoking a method via reflection using parameter names?

查看:77
本文介绍了通过使用参数名称的反射来调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我只是想知道如果我有一组描述参数及其值的

Hi all,
I was just wondering if it would be possible to invoke a method via reflection if I have a set of

KeyValuePair<string, object>

是否可以通过反射调用方法? >
关键是似乎我可能并不总是在方法的签名中拥有参数的位置,因此不可能以正确的顺序为该方法创建参数数组.

是否可以使用参数名称来调用方法?
任何帮助,感激不尽,

欢呼和最诚挚的问候
Andy

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

推荐答案

可以按照http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx [ http: //msdn.microsoft.com/zh-CN/library/de3dhzwy [ ^ ]

可以使用存储在List of KeyValuePairs 中的参数名称和值来调用类的方法,如下所示:
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:
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);
    }
}


是的,这是可能的.您将必须使用更复杂的方法 Type.InvokeMember [ BindingFlags [
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:
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);



问候,

曼弗雷德(Manfred)



Regards,

Manfred


这篇关于通过使用参数名称的反射来调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆