使用一个委托来使用不同参数的几种方法 [英] Using one delegate to for several methods with different parameters

查看:97
本文介绍了使用一个委托来使用不同参数的几种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用一个委托用于不同参数的几种方法?
我使用反射来获取一个类中的所有方法,我想分配每个方法一个委托,并将该委托保存在具有枚举作为键的字典中。
如果对于远程过程调用实现,我正在处理,所以枚举是一个与方法相关的命令。

Is it possible to use one delegate for several methods with different parameters somehow? I use reflection to get all the methods in a class, and I want to assign each of them a delegate and save that delegate in a dictionary with an enum as the key. This if for a remote procedure call implementation I'm working on so the enum is a command associated with a method.

Regards / Per

Regards/Per

推荐答案

可以使用 Delegate.CreateDelegate 函数并传递适当的委托类型( Action<> Func<> )和MethodInfo的一个实例,在这种情况下,你应该构造代理根据MethodInfo参数键入。

You can use the Delegate.CreateDelegate Function and pass the appropriate delegate Type (Action<> or Func<>) and an instance of the MethodInfo, in this case you should construct the delegate type according to the MethodInfo parameters.

这篇关于使用一个委托来使用不同参数的几种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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