我有一个ac#类库用于调试目的,在需要执行任何操作时都将创建方法。
我需要将每种方法自动附加到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();
}
}
也许您想编写一个自定义的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
Enter
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句