如何使 Action与事件的委托类型兼容吗?

发布于 2024-08-08 03:45:15 字数 794 浏览 4 评论 0原文

给出下面的代码:

void LookupBox_Load(object sender, EventArgs e)
{
    Action d = delegate
        {
            if (!_p.AutoClose)
                CloseLookupBox();
        };

    if (this.ParentForm.MdiParent != null)
        this.ParentForm.MdiParent.Deactivate += delegate { d(); };
    else
        this.ParentForm.Deactivate += delegate { d(); };
}

有没有办法省略 delegate { d(); } ?

void LookupBox_Load(object sender, EventArgs e)
{
    Action<object,EventArgs> d = delegate
        {
            if (!_p.AutoClose)
                CloseLookupBox();
        };

    if (this.ParentForm.MdiParent != null)
        this.ParentForm.MdiParent.Deactivate += d;
    else
        this.ParentForm.Deactivate += d;
}

注意:我想内联执行此操作

Given the code below:

void LookupBox_Load(object sender, EventArgs e)
{
    Action d = delegate
        {
            if (!_p.AutoClose)
                CloseLookupBox();
        };

    if (this.ParentForm.MdiParent != null)
        this.ParentForm.MdiParent.Deactivate += delegate { d(); };
    else
        this.ParentForm.Deactivate += delegate { d(); };
}

Is there a way to omit the delegate { d(); } ?

void LookupBox_Load(object sender, EventArgs e)
{
    Action<object,EventArgs> d = delegate
        {
            if (!_p.AutoClose)
                CloseLookupBox();
        };

    if (this.ParentForm.MdiParent != null)
        this.ParentForm.MdiParent.Deactivate += d;
    else
        this.ParentForm.Deactivate += d;
}

Note: I want to do this inline

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

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

发布评论

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

评论(3

往事风中埋 2024-08-15 03:45:15

绝对 - 更改 d 的类型以开始:

EventHandler d = delegate
    {
        if (!_p.AutoClose)
            CloseLookupBox();
    };

匿名方法不仅仅适用于 Func 和 Action...

为了将来的参考,您可以创建一个新的委托基于具有兼容签名的现有代码:

Action<object, EventArgs> d = ...;
EventHandler handler = new EventHandler(d);

但是在这种情况下,这种额外的间接寻址是没有意义的:)

您还可以使用空合并运算符使调用它的代码也稍微简单一些:

Form form = ParentForm.MdiParent ?? ParentForm;
form.Deactivate += d;

因为您只使用 d 一次,您可以内联它,将整个方法变成:

Form form = ParentForm.MdiParent ?? ParentForm;
form.Deactivate += delegate
{
    if (!_p.AutoClose)
        CloseLookupBox();
};

Absolutely - change the type of d to start with:

EventHandler d = delegate
    {
        if (!_p.AutoClose)
            CloseLookupBox();
    };

Anonymous methods don't just work with Func and Action...

For future reference though, you can create a new delegate based on an existing one with a compatible signature:

Action<object, EventArgs> d = ...;
EventHandler handler = new EventHandler(d);

But this extra indirection is pointless in this case :)

You can also make the code calling it slightly simpler too using the null-coalescing operator:

Form form = ParentForm.MdiParent ?? ParentForm;
form.Deactivate += d;

As you're then only using d once, you could inline it, turning the whole method into:

Form form = ParentForm.MdiParent ?? ParentForm;
form.Deactivate += delegate
{
    if (!_p.AutoClose)
        CloseLookupBox();
};
转身以后 2024-08-15 03:45:15

好不了多少,但如果您使用 C# 3.0,您可以这样做:

if (this.ParentForm.MdiParent != null)
    this.ParentForm.MdiParent.Deactivate += (x,y) => d();
else
    this.ParentForm.Deactivate += (x,y) => d();

Not much better, but you could do if you're using C# 3.0:

if (this.ParentForm.MdiParent != null)
    this.ParentForm.MdiParent.Deactivate += (x,y) => d();
else
    this.ParentForm.Deactivate += (x,y) => d();
忘你却要生生世世 2024-08-15 03:45:15

您应该使用 EventHandler来定义这些而不是 Action代表

  EventHandler<EventArgs> d = delegate        
       {            
            if (!_p.AutoClose)                
               CloseLookupBox();        
       };    

You should use EventHandler<MyEventArgs> to define those instead of the Action delegate

  EventHandler<EventArgs> d = delegate        
       {            
            if (!_p.AutoClose)                
               CloseLookupBox();        
       };    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文