c# - 在#endregion 处自动创建同名的#region

标签 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">
<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>

第二次编辑: 它的工作,但只有当我制作插入片段时。从 intellisense 这使用我猜的其他一些片段。

那么有没有办法从智能感知而不是插入片段菜单添加我的区域?

最佳答案

如果你想要实现的是......

#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>

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

与区域的内置代码片段相比,这唯一让您可以灵活地添加尾随注释以指示区域的结束。您还可以通过添加额外的扩展来进一步自定义它。

注意:标记 $end$ 标记操作完成后您希望光标停留的位置。

关于c# - 在#endregion 处自动创建同名的#region,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18122131/

相关文章:

C#WPF使用数据网格从数据库中删除带有主键的项目

c# - 管理 session 数据的更好方法

visual-studio - 自动将 vs2005 调试器附加到子进程

c# - 语义日志分析器异常 WriteEvent 参数和事件参数的数量不同

c# - 基于数据库记录或枚举值的可扩展设计模式

c# - 相对路径在 Windows 服务中不起作用

c# - 需要上传文件,并在 pre-init 事件中使用它

c# - 创建指向目录的符号链接(symbolic link)的问题

visual-studio - 如何在 Visual Studio 2015 中测试 F#?

.net - 是否可以更改 NuGet 包的位置?