C#如何调用参数数量未知的方法 [英] C# How to call a method with unknown number of parameters

查看:188
本文介绍了C#如何调用参数数量未知的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经达到了我的技能极限.我什至不知道这是否可能-但我希望是这样.

I've reached my skills limit here. I don't even know if this is possible - but I hope it is.

我正在制作命令处理程序(文本).对于每个Add(),您可以指定所需参数的数量及其类型.例如:

I am making a command handler (text). For each Add() you specify the number of required parameters and their types. Eg:

void Add(string commandName, int requiredParameters, params Type[] paramTypes) { }
Add("test", 2, typeof(string), typeof(int));

因此,示例命令为:/test hello 7.命令处理程序进行检查以确保类型正确,例如,如果第二个参数不可转换为整数,则它将失败.

So an example command would be: /test hello 7. The command handler checks to make sure the types are right, eg it will fail if the second parameter is not convertible to an int.

现在我遇到的问题是我想在Add()中传递一个方法. (如果所有检查都通过,则命令处理程序将调用此方法,并使用必需的参数调用它).因此,根据Add()中传递的内容,所讨论的方法可以具有任意数量的参数.

Now the problem I'm having is I want to pass a method along in the Add(). (The command handler will call this method if all the checks pass, and calls it with required parameters). So the method in question could have any number of parameters based on what was passed in Add().

我该如何实现?委托不工作,抱怨参数不匹配.我尝试做类似的事情:

How do I achieve this? A delegate doesn't work at it complain about parameters not matching. I've tried doing something like:

void Add<T1, T2>(..., Action<T1, T2> method) { }
Add(..., new Action<string, int>(cmd_MyMethod));

但是我必须为许多类型创建一个Add()方法.例如Add<T1, T2, T3, T4, etc>,并且键入Add()的调用也很麻烦.

But I would have to make an Add() method for a lot of types. Eg Add<T1, T2, T3, T4, etc>, and it also makes it a pain to type the calls to Add().

不想要传递该方法以将其作为字符串调用,然后使用this.GetType().GetMethod()来获取它的句柄.尽管这种方式很理想,但是当我进行混淆处理时,它会变得混乱.

I do not want to pass the method to call as a string, then use this.GetType().GetMethod() to get a handle to it. Although this way would be ideal, it messes up when I do obfuscation.

有人知道有什么方法吗? 预先感谢.

Does anyone know of any way to do this? Thanks in advance.

推荐答案

尝试一下:

void Add(string commandName, int requiredParameters, Delegate method) { }

您可以调用method.DynamicInvoke(...)来调用委托引用的方法.请注意,这将使用反射,因此不会很快.但这很灵活.

You can call method.DynamicInvoke(...) to call the method referenced by the delegate. Note that this will use reflection, so it will not be fast. But it is plenty flexible.

请注意,您仍然必须使用特定类型来构造委托,因此您可能最终会像这样调用它:

Note that you will still have to construct the delegate using a specific type, so you might wind up calling it like this:

Add("test", 2, new Action<string, int>(cmd_MyMethod));

请注意,我省略了Type[]参数,因为您实际上可以从委托引用的MethodInfo中提取它!
(method.Method.GetParameters().Select(p => p.ParameterType).ToArray())

Note that I have omitted the Type[] argument, since you can actually extract this from the MethodInfo referenced by the delegate!
(method.Method.GetParameters().Select(p => p.ParameterType).ToArray())

这篇关于C#如何调用参数数量未知的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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