Automatically Add Regions to Code in Visual Studio

后端 未结 7 1998
轻奢々
轻奢々 2021-02-20 06:09

My team absolutely loves using regions, and with that in mind it\'s pretty much become a de-facto standard in our code. I recently came to realization that I\'m sick of writing

相关标签:
7条回答
  • 2021-02-20 06:46

    And I agree that regions a are a bad idea but to each his own.

    You might want to take a look at NArrange.

    0 讨论(0)
  • 2021-02-20 06:50

    Use the following snippet

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>#Classregion</Title>
          <Shortcut>#Classregion</Shortcut>
          <Description>Code snippet for #Classregion</Description>
          <Author>Author Name</Author>
          <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
          </SnippetTypes>
        </Header>
        <Snippet>
          <Declarations>
            <Literal>
              <ID>name</ID>
              <ToolTip>Region name</ToolTip>
              <Default>MyRegion</Default>
            </Literal>
          </Declarations>
          <Code Language="csharp">
            <![CDATA[
            #region ------------- Members ---------------
            $selected$ $end$
        #endregion
    
      #region --------------- Properties ---------------
            $selected$ $end$
        #endregion
    
      #region --------------- Methods ---------------
            $selected$ $end$
        #endregion
        ]]>
          </Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    Save it under C:\Users\\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets

    Later it will be accessible in C# code, by Right-Click > Insert Snippet > My Code Snippets > #Classregion

    0 讨论(0)
  • 2021-02-20 06:53

    Don't know why so many people speak out against regions; they help me categorize my code very easily. What I use is a macro placed on one of my keyboard buttons that automatically inserts the regions for me. One tip I can give you is to put a small delay between each keypress if this is possible because VS sometimes misses characters otherwise.

    Hope this helps!

    0 讨论(0)
  • 2021-02-20 06:54

    If you have Resharper then the File Structure window is very handy for managing regions, allowing simple drag and drop to move blocks of methods/properties etc. around. It shows your regions (which are collapsible) & syncs with the main code view too:

    https://www.jetbrains.com/help/resharper/Reference__Windows__File_Structure_Window.html

    eg A region called preview shown here:

    Here

    0 讨论(0)
  • 2021-02-20 06:57

    Two ways I know:

    Create a snippet as per this MSDN guide.

    Downloading the Visual Studio Extension Productivity Power Tools which has a "Surround" feature. This surrounds the user made selection with the selected snippet, for example #region #endregion or if statement.

    0 讨论(0)
  • 2021-02-20 07:06

    I prefer editing the class template as described in Sam Harwell's answer:

    https://stackoverflow.com/a/2072717

    You can open the class.cs file at your Visual Studio version's class template location. For example, Visual Studio 2017 Enterprise's location is:

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

    I open that file using a text editor and add regions to it. All new classes will then be created with the regions.

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