单件模板类实现

  using System;

  public class SingletonTemplate < T >
  {
    private static object _locker = new object ();
    private static SingletonTemplate < T > _obj = null ;
    private static T _instance;

    private SingletonTemplate()
    {
      _instance
= Activator.CreateInstance < T > ();
    }
    public static T Instance
    {
      get
      {
        if (_obj == null )
        {
           lock (_locker)
           {
             if (_obj == null )
             {
                _obj
= new SingletonTemplate < T > ();
             }
           }
        }
        return _instance;
      }
    }
  }

转载于:https://www.cnblogs.com/rroo/archive/2011/03/16/1986051.html

你可能感兴趣的:(单件模板类实现)