ReSharper conventions for names of event handlers

前端 未结 4 1011
小鲜肉
小鲜肉 2020-11-29 01:23

When I add new event handler for any event, VS creates method like object_Click. But ReSharper underlines this method as Warning, because all methods should no

相关标签:
4条回答
  • 2020-11-29 01:51

    I just created an extension for Visual Studio 2010, EventHandler Naming, that lets you specify with a simple pattern what you want your generated eventhandler names to be. The default pattern in the extension is On$(SiteName)$(EventName) which will give you event names like OnBtnNameClick instead of btnName_Click. You can get the extension at http://tech.einaregilsson.com/2010/12/22/better-eventhandler-names-in-visual-studio-2010/

    0 讨论(0)
  • 2020-11-29 01:56

    For C# (or VB), make the following change:

    ReSharper | Options | Languages | C# | C# Naming Style, Advanced settings... Change 'Event subscriptions on fields' from $object$_On$event$ to $object$_$event$.

    You may also want to add additional rules to entity kinds like 'Types and namespaces' to account for code-generated classes such as 'Default'. For example, add a new rule with a '' Name Prefix and a Name Style 'UpperCamelCase'.

    0 讨论(0)
  • 2020-11-29 02:00

    Personally, I'd suggest renaming the methods. Generally I think VS comes up with terrible names for both controls and events.

    I prefer to make a method name say what it does, not what calls it. That promotes reuse as well. Admittedly the signature of an event handler is often not ideal for reuse - I'd argue that often a lambda expression calling a method with more sensible parameters would be useful:

    button.Click += (sender, args) => SaveCurrentDocument();
    

    but obviously the designer doesn't support that :(

    Of course, renaming all the methods is going to be more work than just changing the R# settings, if you can find some that work...

    0 讨论(0)
  • 2020-11-29 02:12

    On your file menu you should have "Resharper" Click it -> Options -> Naming conventions (on left menu).

    From there you can specify what naming conventions are used for each of the naming types/styles.

    0 讨论(0)
提交回复
热议问题