如何从 Action 委托创建 MethodInfo
我正在尝试开发一个 NUnit 插件,该插件可以从包含 Action
委托列表的对象动态地将测试方法添加到套件中。问题在于 NUnit 似乎严重依赖反射来完成工作。因此,看起来没有简单的方法可以将我的 Action
直接添加到套件中。
相反,我必须添加 MethodInfo
对象。这通常可以工作,但是 Action
委托是匿名的,因此我必须构建类型和方法来完成此任务。我需要找到一种更简单的方法来做到这一点,而不需要使用 Emit
。有谁知道如何从 Action 委托轻松创建 MethodInfo 实例?
I am trying to develop an NUnit addin that dynamically adds test methods to a suite from an object that contains a list of Action
delegates. The problem is that NUnit appears to be leaning heavily on reflection to get the job done. Consequently, it looks like there's no simple way to add my Action
s directly to the suite.
I must, instead, add MethodInfo
objects. This would normally work, but the Action
delegates are anonymous, so I would have to build the types and methods to accomplish this. I need to find an easier way to do this, without resorting to using Emit
. Does anyone know how to easily create MethodInfo instances from Action delegates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试过 Action 的 Method 属性吗?我的意思是:
Have you tried Action's Method property? I mean something like:
您不需要“创建”
MethodInfo
,您只需从委托中检索它即可:You don't need to "create" a
MethodInfo
, you can just retrieve it from the delegate :抱歉,不是你想要的。
请注意,所有委托都是多播的,因此不能保证具有唯一的
MethodInfo
。这将使您获得所有这些:尽管您丢失了有关目标对象的信息(取消委托),所以我不希望 NUnit 之后能够成功调用任何内容。
Sorry, not what you wanted.
Note that all delegates are multicast, so there isn't guaranteed to be a unique
MethodInfo
. This will get you all of them:You're losing the information about the target objects though (uncurrying the delegate), so I don't expect NUnit to be able to successfully call anything afterwards.