Reputation: 2127
I have a c# class library for my debugging purpose where I will create methods whenever I need to do something.
Each and every method I need to append to the try catch and some other code snippet automatically. Say for ex. as below, when I create a new method, the try/catch and the snippet inside should be created automatically for each and every method when I create.
Any idea how I can do this?
public static void MyMethod1()
{
try
{
string loggerFileName = System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
Upvotes: 3
Views: 484
Reputation: 13438
Maybe you want to write a custom visual studio code snippet. I just took some different snippet and modified it to your request... it's not tested, so may need some minor changes in order to work. just tested it, working like a charm for me.
Create a test.snippet
file
<?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>
Then install it in VS Tools - Snippet Manager.
Use it as writing test
TAB+TAB YourActuallyDesiredMethodName
Enter
Upvotes: 5
Reputation: 5150
How about
Writing svm
and then press TAB+TAB (creates static method)
Writng try
and then press TAB+TAB (creates try-catch block)
The result should look like this:
static void Main(string[] args)
{
try
{
}
catch (Exception)
{
throw;
}
}
A new method with try-catch in 10 keystrokes :)
Upvotes: 1