简体   繁体   English

是否可以在visual studio中更改自动生成的事件处理程序代码?

[英]Is it possible to alter the auto generated event handler code within visual studio?

I would like to be able to control the default code that's generated for event's when I use one of Visual Studio's automatically generated blocks. 我希望能够控制当我使用Visual Studio的一个自动生成的块时为事件生成的默认代码。 The current template is as follows: 目前的模板如下:

void HandlerName(object sender, HandlerEventArgs e) {
    throw new NotImplementedException();
}

I would like to change this to the following: 我想将此更改为以下内容:

private void HandlerName(object sender, HandlerEventArgs args) {
    throw new NotImplementedException();
}

Namely it's the args argument that I always change. 也就是说,这是我经常改变的args论点。

UPDATE : Further to this it is policy that we also include comments for private members here, thus another use-case for my requirement is to also generate the default comment. 更新 :除此之外,我们还在此处包含对私有成员的评论的政策,因此我的要求的另一个用例是生成默认评论。

UPDATE 2 : I now retract the reasoning for wanting to rename e to args due to evidence of a non-standard naming convention, however I still would like to override the template if possible for explicit access modifier and default comments. 更新2 :我现在收回由于非标准命名约定的证据而想要将e重命名为args的推理,但是我仍然希望覆盖模板,如果可能的话,显式访问修饰符和默认注释。

我认为e来自委托签名( delegate void HandlerEventHandler(object sender, HandlerEventArgs e) ),所以你不能在不改变委托签名的情况下改变它...

In the case you still want to change something to the auto-generated code for event handler, I use this extension for Visual Studio since a couple of years. 如果您仍想将某些内容更改为自动生成的事件处理程序代码,那么我将在几年后将此扩展用于Visual Studio。

It's very helpful to generate more clean event handler name. 生成更干净的事件处理程序名称非常有用。 As you will see, instead of generating something like txtName_LostFocus, the extension will suggest to you a method with name like this: OnNameLostFocus. 正如您将看到的,扩展程序将向您建议一个名称如下的方法:OnNameLostFocus,而不是生成类似于txtName_LostFocus的内容。

This help getting rid of underscore and prefixes that we usually add to controls. 这有助于摆脱我们通常添加到控件的下划线和前缀。 The extension have a configuration screen to add all the prefixes that you want. 扩展程序有一个配置屏幕,用于添加所需的所有前缀。

But everything I say is not helpful for answering your question. 但我说的一切对回答你的问题没有帮助。 Here is where it help -> In the code that you can download, you can see how the engine works to replace the name of the event handler method and I'm totally sure that you can found a solution to rename any argument you want (if you still want) and also, add an explicit modifier. 这是它的帮助 - >在您可以下载的代码中,您可以看到引擎如何工作以替换事件处理程序方法的名称,我完全相信您可以找到一个解决方案来重命名您想要的任何参数(如果你仍然想要)并且还添加一个显式修饰符。

I will try to find the solution and let you know of my progress. 我会尝试找到解决方案并告诉您我的进度。

UPDATE: You can also use Resharper who will automatically if you want add explicit modifier for you using the clean up engine. 更新:如果你想使用清理引擎为你添加显式修饰符,你也可以自动使用Resharper。

Are you saying you want to change the auto-generated code when you create an event handler? 您是否说要在创建事件处理程序时更改自动生成的代码?

My answer is no, but I am not really sure why you would need to. 我的回答是否定的,但我不确定你为什么需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM