自动将区域添加到Visual Studio中的代码 [英] Automatically Add Regions to Code in Visual Studio
问题描述
我的团队绝对喜欢使用区域,因此,在我们的代码中,它几乎已成为事实上的标准.最近我意识到,每次创建类,测试方法等时,我都讨厌编写或ctrl + c/ctrl + v ...
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 or ctrl+c / ctrl+v'ing these in every time I create a class, test method, etc...
我想知道Visual Studio是否有可能(通过宏或某些其他功能)自动将它们添加到您的代码中.
I was wondering if it is possible (via macros or some other functionality) to have Visual Studio automatically add these into your code.
例如,如果我在项目中添加了一个新的类文件,您是否可以通过某种魔术使Visual Studio生成文件,如下所示:
For example, If I add a new class file to my project, can you perform some sort of magic to have visual studio generate the file as:
namespace Test
{
class MyClass
{
#region ------------ members --------------
#endregion
#region ------------ properties --------------
#endregion
#region ------------ methods --------------
#endregion
}
}
我目前不知道如何执行此操作使我很烦恼,这是在编写单元测试时.这可能有点棘手,但是我试图找到一种自动添加--set up--
和--run test--
区域以测试方法的方法,因为我们的团队坚持使用它们.
Where I really get annoyed by not currently knowing how to do this, is when I'm writing unit tests. This may be a bit trickier, but I was trying to find a way to add --set up--
and --run test--
regions automatically to test methods because our team is adamant about using them.
所以,当我去创建一种新的测试方法时
So, when I go to create a new test method
[TestMethod]
public void WhenCondition_WillProduceExpectedResult()
{
}
Visual Studio会自动将这两个区域添加到方法中,例如:
Visual Studio would automatically add these two regions to the method, such as:
[TestMethod]
public void WhenCondition_WillProduceExpectedResult()
{
#region ------------- set up -------------
#endregion
#region ------------- run test -------------
#endregion
}
不确定是否可以这样做,是否可以通过vs-macro或扩展名.任何帮助深表感谢!
Not sure if this can be done, and if it can, whether it'd be via a vs-macro, or extension. Any help is much appreciated!
推荐答案
您可以创建一个简单的代码段,如下所示:
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>
将该文件保存到C:\Users\<your_user>\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets
.
现在,您只需要重新打开Visual Studio,在类中键入"simple",然后按两次Tab键即可.
Now you just need to reopen Visual Studio, type 'simple' into a class and press Tab key twice.
这篇关于自动将区域添加到Visual Studio中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!