一个泛型的单例模式

 public static class SingleInstanceFactory
        {
            
private static Hashtable ht = new Hashtable();
            
private static object syncObject = new object();

            
public static T GetSingleInstance<T>() where T : new()
            {
                
string key = typeof(T).ToString();

                T t 
= default(T);

                
lock (syncObject)
                {

                    
if (ht.ContainsKey(key))
                    {
                        t 
= (T)ht[key];
                    }
                    
else
                    {
                        
lock (syncObject)
                        {
                            t 
= Activator.CreateInstance<T>();
                            ht.Add(key, t);
                        }
                    }
                }

                
return t;
            }
        }
原文地址:https://www.cnblogs.com/sskset/p/2097026.html