重写 lambda 扩展方法

发布于 2024-08-19 17:58:32 字数 697 浏览 4 评论 0原文

我创建了一个扩展方法,它的工作原理就像我想要的那样。我注意到,partyproperty 参数以某种方式被“复制”到 lambda 表达式中。这样我就不需要维护编辑器/政党/属性关联的自定义列表。

但是,我需要重置 ButtonEdit 的 ButtonClick 事件。由于这是匿名的,我也无法使用 -= 运算符。

所以,我的问题是 - 如何重写这个方法以便可以删除委托?或者,我可以使用哪种其他方法来处理具有额外参数(例如 partyproperty)的特定事件处理程序?

private static void SetupAddressButtonClickEvent(this ButtonEdit editor, Party party, string property)
{
    editor.SetAddressDisplayText(party, property);
    editor.ButtonClick += (sender, e) =>
        {
            party.ShowAddressLookupDialog(property);
            editor.SetAddressDisplayText(party, property);
        };
}

谢谢你, 斯特凡

I've created an extension method that works just like I wanted. I've noticed that somehow the party and property parameters are 'copied' into the lambda expression. This way I do not need to maintain a custom list of editor/party/property associations.

However, I need to reset the ButtonEdit's ButtonClick event. Since this one is anonymous I cannot use the -= opertor either.

So, my question is - how do I rewrite this method so that the delegate can be removed? Or, which other approach can I use to handle a specific event handler with extra parameters (such as party and property)?

private static void SetupAddressButtonClickEvent(this ButtonEdit editor, Party party, string property)
{
    editor.SetAddressDisplayText(party, property);
    editor.ButtonClick += (sender, e) =>
        {
            party.ShowAddressLookupDialog(property);
            editor.SetAddressDisplayText(party, property);
        };
}

Thank you,
Stefan

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

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

发布评论

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

评论(1

翻了热茶 2024-08-26 17:58:32
Action<object,EventArgs> myaction = (sender, e) =>
        {
            party.ShowAddressLookupDialog(property);
            editor.SetAddressDisplayText(party, property);
        };

editor.ButtonClick += myaction;
editor.ButtonClick -= myaction;

编辑选项 2 可以是:

class MyEventHandler
{
  ... _property;
  ... _party;
  ... _editor;
  public MyEventHandler(... property, ... party, ... editor)
  {
    _property = property;
    _party = party;
    _editor = editor;
  }

  public void Handler(object sender, EventArgs e)
  {
    _party.ShowAddressLookupDialog(_property);
    _editor.SetAddressDisplayText(_party, _property);
  }
}

然后像这样使用它:

var handler = new MyEventHandler(party,property,editor);
editor.ButtonClick += handler.Handler;

我不确定这会对您有多大帮助,因为我不 100% 理解您要解决的问题。

Action<object,EventArgs> myaction = (sender, e) =>
        {
            party.ShowAddressLookupDialog(property);
            editor.SetAddressDisplayText(party, property);
        };

editor.ButtonClick += myaction;
editor.ButtonClick -= myaction;

edit option 2 could be:

class MyEventHandler
{
  ... _property;
  ... _party;
  ... _editor;
  public MyEventHandler(... property, ... party, ... editor)
  {
    _property = property;
    _party = party;
    _editor = editor;
  }

  public void Handler(object sender, EventArgs e)
  {
    _party.ShowAddressLookupDialog(_property);
    _editor.SetAddressDisplayText(_party, _property);
  }
}

and then use it like this:

var handler = new MyEventHandler(party,property,editor);
editor.ButtonClick += handler.Handler;

I'm not sure how much this will help you because I don't 100% understand what you're trying to solve.

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