C#中使用反射调用方法时如何对参数进行自动类型转换?
我需要使用 C# 通过反射调用类型上的方法。
在运行时,我的数据将由包含名称/值对的字典组成。字典中的名称将与我将调用的方法上的参数名称相对应。另外,在运行时,我将拥有任意程序集限定类型名称和方法名称。在设计时,我对类型和方法一无所知,除了该方法将接受可变数量的 int、string、DateTime、bool、int[]、string[]、DateTime[] 或 bool 类型的参数[]。
我可以毫无问题地使用反射创建该类型的实例并调用该方法。当我调用时,我陷入了必须将字典中的字符串值转换为方法所需的适当类型的境地:
someMethodInfo.Invoke(instance, new [] { ... })
我知道我可能需要通过 MethodInfo.GetParameters() 进行枚举并为每个值执行类型转换范围。我想要弄清楚的是如何做到这一点,理想情况下,如何有效地做到这一点。
到目前为止,我的研究涉及深入研究 MVC 源代码,因为它在将表单值传递给 ActionMethod 时执行类似的操作。我发现 ActionMethodDispatcher 但它使用我不熟悉的 LINQ 表达式。
我也在 SO 上查看了类似的问题,但没有找到任何可以回答我的问题的内容。
我欢迎任何有关解决方案的指示。
I have a need to invoke methods on a type through reflection using C#.
At run-time, my data will consist of a Dictionary containing name/value pairs. The names in the Dictionary will correspond to parameter names on the method I will invoke. Also, at run-time, I will have an arbitrary assembly qualified type name and a method name. At design time, I will have no knowledge of the type and the method other than that the method will accept a variable number of parameters of type int, string, DateTime, bool, int[], string[], DateTime[] or bool[].
I have no problem getting to the point where I can create an instance of the type using reflection and invoke the method. I am stuck at the point where I have to convert the string values in my dictionary to the appropriate type needed by the method when I call:
someMethodInfo.Invoke(instance, new [] { ... })
I know that I need to probably enumerate through MethodInfo.GetParameters() and perform the type conversion for each parameter. What I am trying to figure out is how to do this, and ideally, how to do it efficiently.
My research so far has involved digging into the MVC source code as it does something similar when passing form values to an ActionMethod. I found ActionMethodDispatcher but it uses LINQ Expressions, with which I am unfamiliar.
I also looked at similar questions on SO, but did not find anything that answers my question.
I would welcome any pointers to a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一些可用于参数转换的代码:
它支持基本类型、可空值和基本类型数组。
如果您要使用不支持 IConvertible 接口的类型 - 最好为每个单独的类型实现自定义转换器。
可以使用 Linq 以更优雅的方式编写它。
维塔利
Here is some code which can be used for parameters conversion:
It supports Primitive types, Nullables and Arrays of primitive types.
In the case when you going to use types which doesn't support IConvertible interface - it is better to implement custom converters for each individual type.
It can be written in more elegant way with Linq.
Vitaliy
要转换的值应该是一个对象,否则标准类型之外的转换将不起作用。您可以轻松地在类型之间进行转换,如下所示:
就这么简单。如果你想要一个方法:
The value you want to convert should be an object, otherwise conversions outside the standard types will not work. You can easily convert between types like so:
It's as easy as that. If you want a method:
也许管理“转换器”的一个好方法是维护一个
Dictionary
- 其中IMyTypeConverter
有一个object Convert(string value)
。Perhaps a nice way to manage "converters" is to maintain a
Dictionary<Type, IMyTypeConverter>
- whereIMyTypeConverter
has aobject Convert(string value)
.