如何从 Action 委托创建 MethodInfo

发布于 2024-08-28 01:27:30 字数 333 浏览 4 评论 0原文

我正在尝试开发一个 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 Actions 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里°也失望 2024-09-04 01:27:30

您尝试过 Action 的 Method 属性吗?我的意思是:

MethodInfo GetMI(Action a)
{
    return a.Method;
}

Have you tried Action's Method property? I mean something like:

MethodInfo GetMI(Action a)
{
    return a.Method;
}
乱了心跳 2024-09-04 01:27:30

您不需要“创建”MethodInfo,您只需从委托中检索它即可:

Action action = () => Console.WriteLine("Hello world !");
MethodInfo method = action.Method

You don't need to "create" a MethodInfo, you can just retrieve it from the delegate :

Action action = () => Console.WriteLine("Hello world !");
MethodInfo method = action.Method
我是男神闪亮亮 2024-09-04 01:27:30
MethodInvoker CvtActionToMI(Action d)
{
   MethodInvoker converted = delegate { d(); };
   return converted;
}

抱歉,不是你想要的。

请注意,所有委托都是多播的,因此不能保证具有唯一的 MethodInfo。这将使您获得所有这些:

MethodInfo[] CvtActionToMIArray(Action d)
{
   if (d == null) return new MethodInfo[0];
   Delegate[] targets = d.GetInvocationList();
   MethodInfo[] converted = new MethodInfo[targets.Length];
   for( int i = 0; i < targets.Length; ++i ) converted[i] = targets[i].Method;
   return converted;
}

尽管您丢失了有关目标对象的信息(取消委托),所以我不希望 NUnit 之后能够成功调用任何内容。

MethodInvoker CvtActionToMI(Action d)
{
   MethodInvoker converted = delegate { d(); };
   return converted;
}

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:

MethodInfo[] CvtActionToMIArray(Action d)
{
   if (d == null) return new MethodInfo[0];
   Delegate[] targets = d.GetInvocationList();
   MethodInfo[] converted = new MethodInfo[targets.Length];
   for( int i = 0; i < targets.Length; ++i ) converted[i] = targets[i].Method;
   return converted;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文