3

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();
}
}
2

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks grek40. It works for me. This is what I was looking for.
1

How about

  1. Writing svm and then press TAB+TAB (creates static method)

  2. Writng tryand 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 :)

1 Comment

Thanks Smartis. I am aware of this 2.Writng tryand then press TAB+TAB (creates try-catch block)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.