Automatically Add Regions to Code in Visual Studio

青春壹個敷衍的年華 提交于 2019-12-04 02:56:07

You could create a simple code snippet like the following one:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Simple</Title>
      <Shortcut>simple</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>name</ID>
          <ToolTip>Replace with the name of the action</ToolTip>
          <Default>Action</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
        public void $name$()
        {
            #region ------------- set up -------------
            #endregion 

            #region ------------- run test -------------
            #endregion 
        }
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Save that file into C:\Users\<your_user>\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets.

Now you just need to reopen Visual Studio, type 'simple' into a class and press Tab key twice.

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.

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!

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

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

You might want to take a look at NArrange.

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:

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.

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