如何在 C#2 中创建具有 3 个参数的委托方法?

发布于 2024-10-06 20:30:17 字数 837 浏览 6 评论 0原文

我有以下方法:

    private void WriteTrace(object sender, EventArgs e, EventElement eventElement)
    {
    /* ... */
    }

当我想像这样创建委托时:

Type controlType = control.GetType();
MethodInfo method = typeof(Trace).GetMethod("WriteTrace", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
EventInfo eventInfo = type.Value.GetType().GetEvent("Load");  // for the sample, we suppose the control is a form.
Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, method);
eventInfo.AddEventHandler(control, handler);

该行

Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, method);

生成错误:

绑定到目标方法时出错

但我不知道为什么?

问候,

弗洛里安

I have the following method :

    private void WriteTrace(object sender, EventArgs e, EventElement eventElement)
    {
    /* ... */
    }

When I want to create Delegate like this :

Type controlType = control.GetType();
MethodInfo method = typeof(Trace).GetMethod("WriteTrace", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
EventInfo eventInfo = type.Value.GetType().GetEvent("Load");  // for the sample, we suppose the control is a form.
Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, method);
eventInfo.AddEventHandler(control, handler);

The line

Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, method);

generates an error :

Error binding to target method

But I don't know why ?

Regards,

Florian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

入怼 2024-10-13 20:30:17

Form.Load 的类型是 EventHandler,它只有两个参数,而不是三个。换句话说,这也会失败:

EventHandler x = new EventHandler(WriteTrace);

您无法从参数数量错误的方法创建事件处理程序。 (实际上,创建像这样的开放委托有一些技巧,但它们与这里无关,IMO。)

您真正想要实现什么?在这种情况下,您期望 eventElement 是什么?

请注意,您可以使用 lambda 表达式来捕获变量,例如,

EventElement element = ...;
form.Load += (sender, args) => WriteTrace(sender, args, element);

这对您有帮助吗?或者您真的需要通过反思来做到这一点吗?

The type of Form.Load is EventHandler, which only has two parameters, not three. In other words, this would fail too:

EventHandler x = new EventHandler(WriteTrace);

You can't create an event handler from a method with the wrong number of parameters. (Actually there are some tricks around creating open delegates like this, but they're not relevant here, IMO.)

What are you really trying to achieve? What would you expect eventElement to be in this case?

Note that you can use lambda expressions to capture variables, e.g.

EventElement element = ...;
form.Load += (sender, args) => WriteTrace(sender, args, element);

Does that help you at all? Or do you really need to do this with reflection?

匿名。 2024-10-13 20:30:17

简而言之,您不能直接将事件处理程序绑定到具有三个参数的方法;您传入的方法必须与签名完全匹配。

问题是,这个“EventElement”参数从哪里来?一旦您知道了这一点,您就可以将其存储在另一个对象中,ala:

class HandlerThunk {
  EventElement eventElement;
  Trace receiver;

  // ...

  public void Handler(object sender, EventArgs e) {
    this.receiver.WriteTrace(sender, e, this.eventElement);  
  }
}

...然后绑定它...

HandlerThunk thunk = ...;

Type controlType = control.GetType();
MethodInfo method = typeof(HandlerThunk).GetMethod("WriteTrace", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
EventInfo eventInfo = type.Value.GetType().GetEvent("Load");  
Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, thunk, method);
eventInfo.AddEventHandler(control, handler);

然后 thunk 会将事件转发给您。

The short answer is you cannot directly bind the event handler to a method with three parameters; the method you pass in must match the signature exactly.

The question is, where does this "EventElement" parameter come from? Once you know that, you can squirrel it away in another object, ala:

class HandlerThunk {
  EventElement eventElement;
  Trace receiver;

  // ...

  public void Handler(object sender, EventArgs e) {
    this.receiver.WriteTrace(sender, e, this.eventElement);  
  }
}

...and then bind it...

HandlerThunk thunk = ...;

Type controlType = control.GetType();
MethodInfo method = typeof(HandlerThunk).GetMethod("WriteTrace", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
EventInfo eventInfo = type.Value.GetType().GetEvent("Load");  
Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, thunk, method);
eventInfo.AddEventHandler(control, handler);

And then the thunk will forward the event to you.

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