Can I customize automatic event handler generation in Visual Studio?

无人久伴 提交于 2019-11-27 12:06:27

问题


When you subscribe to an event in code, Visual Studio automatically completes the code after += and generates the appropriate event handler:

button.Click += new EventHandler(button_Click);
//              ↑_____auto generated code_____↑

Notice how it explicitly creates the delegate instance: even though method groups are implicitly convertible to delegates since C# 2, the IDE behavior still hasn't changed in VS2010.

So I'd like to know, is there a way to generate code like this instead?

button.Click += button_Click;

EDIT
just to make things clear to everyone: the code above is not in a designer file (I wouldn't care which syntax is used if it was the case). It's the snippet that is triggered when you type += after an event name and press TAB


EDIT2
I reported this as a suggestion on Connect, you can vote for it if you also want the current behavior to be changed


回答1:


The suggestion on Connect has been marked as fixed, so the new behavior should be included in the next public build of VS11.

EDIT: just checked, it is indeed included in the beta.




回答2:


Thanks for clarifying your question. Unfortunately, there isn't a way to configure or customize the event hookup code that is generated by the C# language service. To make matters worse, the Generate Method Stub feature won't work on an event hookup either -- though it should, and that is already fixed for the next release of Visual Studio.

Sorry that there isn't a better story for this particular feature. :-(




回答3:


I use this extension for Visual Studio since a couple of years.



来源:https://stackoverflow.com/questions/4471593/can-i-customize-automatic-event-handler-generation-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!