C# 在#endregion自动创建同名的#region

C# 在#endregion自动创建同名的#region,c#,.net,visual-studio,resharper,code-snippets,C#,.net,Visual Studio,Resharper,Code Snippets,我想知道是否有一种方法可以使#region Some region#endregion Some region。 如果没有办法做到这一点,那么使用Resharper可能吗 希望我在这里想要达到的目标是明确的 编辑: <?xml version="1.0" encoding="utf-8" ?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <Cod

我想知道是否有一种方法可以使
#region Some region#endregion Some region
。 如果没有办法做到这一点,那么使用Resharper可能吗

希望我在这里想要达到的目标是明确的

编辑:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>#region</Title>
        <Shortcut>#region</Shortcut>
        <Description>Code snippet for #region</Description>
        <Author>Microsoft Corporation</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 $name$
    $selected$ $end$
#endregion $name$]]>
        </Code>
    </Snippet>
</CodeSnippet>
</CodeSnippets>

#区域
#区域
#区域的代码段
微软公司
膨胀
周边地区
名称
地区名称
MyRegion


第二次编辑: 这是工作,但只有当我插入代码段。来自intellisense,使用其他一些代码片段

那么,有没有一种方法可以从intellisense而不是从insert snippet菜单添加我的区域?

您不需要这样做

您可以这样做:

#region Some Region
//I
//am
//assuming
//a
//lot
//of
//code
//you
//want
//to
//hide
//goes
//here
//here
#endregion
//note that it doesn't say Some Region in the endregion

您可以将#区域的ReSharper默认模板更改为:

#region $name$
    $END$
#endregion $name$
更新:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>#region</Title>
        <Shortcut>#region</Shortcut>
        <Description>Code snippet for #region</Description>
        <Author>Microsoft Corporation</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 $name$
    $selected$ $end$
#endregion $name$]]>
        </Code>
    </Snippet>
</CodeSnippet>
</CodeSnippets>

奇怪,但如果更改默认的区域模板,则什么都不起作用。您需要定义自己的模板,为其设置一个代码段(即reg),并将上面编写的代码放入其中。

如果您试图实现的是

#region MyRegion
//...lots of code...
#endregion // end of MyRegion
您可以使用所谓的“SurroundsWith”代码段来实现这一点。这是我的图书馆里的一个片段

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0"    
   xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Enregions a block of code</Title>
    <Author>GJV</Author>
    <Shortcut>enr</Shortcut>
    <Description>Surrounds a block of code with region directives</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal Editable="True">
        <ID>RegionName</ID>
        <ToolTip>Region Name</ToolTip>
        <Default>MyRegion</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">  
    <![CDATA[
    #region $RegionName$
    $end$         
    $selected$    
    #endregion // end of $RegionName$ 
    ]]>        
    </Code>
  </Snippet>
</CodeSnippet>

注册一段代码
GJV
安然
用区域指令包围代码块
周边地区
膨胀
地区名称
地区名称
MyRegion


要在Visual Studio中使用它,请将代码段放在.snippet文件中并保存在代码段目录中,然后转到工具=>代码段管理器=>添加。添加后,您可以使用标准的CTRK K+X访问它

在区域的内置代码段之外,这为您提供的唯一功能是添加尾部注释以指示区域结束的灵活性。您还可以通过添加额外的扩展来进一步定制

注意:sentinal$end$标记操作完成时光标要落在的位置。

我推荐

看一看“代码块结束标记器改进”部分

2014年8月25日编辑

它将放置代码块的开头(方法名aso)
作为代码块末尾的浅灰色超链接。作为超链接,因为它是可单击的,您可以导航到代码块的开头。

Visual Studio的内置版本是Ctrl K+X

Visual Studio 2017

键入#r TAB,然后键入区域名称


这是内置的行为。

在Visual studio中按Control+K,S并选择Region

可以通过选择文本->右键单击->环绕->RegionI guess VS不支持这一点,我认为这不是一个好主意。如果您更改了区域的名称,您可能会忘记在
#endregion
部分中这样做,从而造成混乱的不一致。我想他希望结束区域指定区域名称?@christiandev我知道,但为什么?这没什么区别,除非他想做这样的事情:
#区域1#区域2#结束区域1#结束区域2
,这只会导致VS一大堆混乱。对于一个大类,区域通常很大,并且有#结束区域指示它对应的区域(因为区域的开始可能在屏幕外)可以帮助您识别代码中的当前位置。我不认为OP试图嵌套/重叠区域。我认为OP试图做一些类似于在块的右括号后可能留下的注释的事情,例如,
if(x){…}//end if(x)
。。。但想象一下,这具尸体又大又多。这对编译器没什么帮助;只有键盘后面的人。如何使用这个覆盖模板?我这样做了,但仍然使用旧的:(更新的答案。尝试创建新模板。我已经为区域编辑了内置模板,但似乎不起作用;/I使用如何更改它编辑我的帖子,这不会按照问题的要求在endregion中添加相同的名称。虽然这样做有效,但不会在endregion中添加区域名称