C# VS如何在创建新方法时自动添加自定义代码段?
我有一个用于调试的c#类库,每当我需要做一些事情时,我都会在其中创建方法 我需要自动附加到try-catch和其他一些代码段的每个方法。例如,如下所示,当我创建一个新方法时,应该在创建时为每个方法自动创建try/catch和其中的代码段 你知道我该怎么做吗C# VS如何在创建新方法时自动添加自定义代码段?,c#,visual-studio,C#,Visual Studio,我有一个用于调试的c#类库,每当我需要做一些事情时,我都会在其中创建方法 我需要自动附加到try-catch和其他一些代码段的每个方法。例如,如下所示,当我创建一个新方法时,应该在创建时为每个方法自动创建try/catch和其中的代码段 你知道我该怎么做吗 public static void MyMethod1() { try { string loggerFileName = System.Reflection.MethodBase.GetCurrentMethod()
public static void MyMethod1()
{
try
{
string loggerFileName = System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
怎么样
svm
,然后按TAB+TAB(创建静态方法)try
,然后按TAB+TAB(创建try-catch块)static void Main(string[] args)
{
try
{
}
catch (Exception)
{
throw;
}
}
一个新方法,在10次击键中使用try-catch:)也许您想编写一个自定义的visual studio代码段。我只是取了一些不同的片段并根据你的要求修改了它。。。它未经测试,因此可能需要一些小的更改才能正常工作。刚刚测试过,对我来说很有魅力 创建一个
test.snippet
文件
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Test Method</Title>
<Author>Grek40</Author>
<Description>Create a testmethod with initial body</Description>
<Shortcut>test</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>name</ID>
<ToolTip>Replace with the testmethod name</ToolTip>
<Default>TestMethod</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
public static void $name$()
{
try
{
string loggerFileName = System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
$end$
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
试验方法
格雷克40
创建带有初始主体的testmethod
测试
名称
替换为testmethod名称
测试方法
然后将其安装在VS工具-代码段管理器中
将其用作编写
test
TAB+TABYourActuallyDeiredMethodName
Enter可能就是您要查找的内容。如果您使用的是Resharper,它的功能非常强大且可自定义。您是否尝试创建自己的Visual Studio代码段?谢谢Smartis。我知道这2。写尝试,然后按TAB+TAB(创建尝试捕捉块)谢谢grek40。它对我有用。这就是我要找的。