与初始化语法初始化事件 [英] Initializing events with initializer syntax

查看:140
本文介绍了与初始化语法初始化事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常要这样写:

new Form
{
    Text = "Caption",
    Controls =
    {
        new Button { Text = "Button 1", Click = (s, e) => MessageBox.Show("Button 1 Clicked"), Location = new Point(10, 10) },
        new Button { Text = "Button 2", Click = new EventHandler(Button2Clicked), Location = new Point(10, 40) },
        new Button { Text = "Button 3", Click = Button3Clicked, Location = new Point(10, 70) },
    },
}

初​​始化语法只是糖分,所以为什么不能编译弄清楚如何生成code为事件订阅?

Initializer syntax is just sugar, so why can't the compiler figure out how to generate code for an event subscription?

给我一些糖,宝贝!

在初始化语法被发明出来,一定有人想过事件,并拒绝了他们。我一直在试图想象的理由可能是和我来了空白。

When initializer syntax was invented, someone must have thought about events and rejected them. I've been trying to imagine what the rationale might have been and am coming up blank.

是不是因为一个事件是多投的对象可能有多个用户?没有,这是一个初始化过程;不可能有任何其他的用户。 [更新] 事实并非如此,初始化应用后的建筑和一个对象的订阅自己的事件的。

Is it because an event is a multi-cast object that might have more than one subscriber? No, this is an initialization process; There can be no other subscribers. [Updated] Not true, initializers are applied post-construction and an object can subscribe to its own events.

一个音符埃里克:我听说过的为什么不C#实现功能X 的演讲。在这种情况下,有人已经在那里,执行初始化

A note to Eric: I've heard the Why doesn't C# implement feature X speech. In this case, someone was already there, implementing initializers.

更新

有似乎是因为我使用的争/混乱点击= 在我的例子。实际语法不相关的问题。它可以很容易被点击+ = 这反映你​​有正常添加处理程序的方式。我preFER前者,因为它是一致的与初始化语法的休息,但最终我不在乎,只是只要我能在初始化列表订阅事件。

There seems to be contention/confusion because I used Click = in my example. The actual syntax is not relevant to the question. It could just as easily be Click += which mirrors the way you have to add a handler normally. I prefer the former because it's consistant with the rest of the initializer syntax, but ultimately I don't care, just so long as I can subscribe to an event in an initializer list.

另一个更新

我也知道添加功能现在大概是不可能的。我所想到的第一个问题是,智能感知必须更新。可能有许多会妨碍现在添加此功能其他的东西。我的问题是:他们为什么不添加它摆在首位。一定有什么引人注目的是warrented了反对票。

I do realize that adding the feature now is probably unlikely. The first issue that comes to mind is that Intellisense has to be updated. There are probably many other things that would hinder adding this feature now. My question is: Why didn't they add it in the first place. There must have been something compelling that warrented the 'nay' vote.

推荐答案

我不明白为什么他们不能提供糖的这个小茶匙任何理由,我想他们只是没有!

I cannot see any reason why they could not have provided this small teaspoon of sugar, I guess they just didn't!

有已经是相当多的参与活动,如果简单地声明一个类事件,而无需提供自己的实现,编译器为您提供的委托支持字段,以及添加/删除方法的实现语法糖。此外,当您添加事件处理程序,编译器使用委托推断,可以方便地将指向一个方法,而不是创建一个委托的重新presents方法。

There is already quite a lot of syntactic sugar involved in events, if simply declare an event on a class without providing your own implementation, the compiler is providing a delegate backing field for you, plus add / remove 'method' implementations. ALso, when you add an event handler, the compiler uses delegate inference, allowing you to simply point to a method, rather than create a delegate that represents the method.

有趣的是,单C#确实允许您在一个对象初始化添加事件处理程序:

Interestingly, Mono C# does allow you to add an event handler in an object initializer:

<一个href=\"http://tirania.org/blog/archive/2009/Jul-27-1.html\">http://tirania.org/blog/archive/2009/Jul-27-1.html

时间切换到单声道; - )

Time to switch to Mono ;-)

这篇关于与初始化语法初始化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆