开发者

Create class that implements a particular design pattern with ReSharper

开发者 https://www.devze.com 2023-02-20 01:19 出处:网络
Design patters are a point of arguments between developers, but I use them very often.开发者_开发百科 Even more, I use one of the most controversial of all common patterns - singleton.

Design patters are a point of arguments between developers, but I use them very often.开发者_开发百科 Even more, I use one of the most controversial of all common patterns - singleton.

Here is how I implement it in C#:

class MySingletonClass
{
    private static volatile MySingletonClass _instance;
    private static object syncRoot = new Object();

    public static MySingletonClass Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (syncRoot)
                {
                    if (_instance == null)
                        _instance = new MySingletonClass();
                }
            }
            return _instance;
        }
    }

    private MySingletonClass()
    {

    }
}

I want to be able to automatically create singleton classes while developing, with only entering a name for the class.

How can I implement this functionality with ReSharper? Can ReSharper pattern catalog be used for this?

Thank you.


You can create a Code Template for it (a File Template if you wish every singleton in a new file).

There are instructions on how to create a template from scratch and from existing code in the help files.

0

精彩评论

暂无评论...
验证码 换一张
取 消