我有一个c#类库用于我的调试目的,每当我需要做某事时我都会创建方法。
我需要自动附加到try catch和其他一些代码段的每个方法。说对于前。如下所示,当我创建一个新方法时,应该在创建时为每个方法自动创建try / catch和里面的代码片段。
知道我该怎么做吗?
public static void MyMethod1()
{
try
{
string loggerFileName = System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
答案 0 :(得分:5)
也许你想写一个自定义的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>
然后将其安装在VS Tools - Snippet Manager中。
将其用作test
TAB + TAB YourActuallyDesiredMethodName
输入
答案 1 :(得分:1)
怎么样
写svm
然后按 TAB + TAB (创建静态方法)
写入try
,然后按 TAB + TAB (创建try-catch块)
结果应如下所示:
static void Main(string[] args)
{
try
{
}
catch (Exception)
{
throw;
}
}
在10次击键中使用try-catch的新方法:)