1 using System; 2 3 public class Singleton<T> where T : class, new() 4 { 5 private static T _Instance; 6 7 public static T Instance 8 { 9 get 10 { 11 if (Singleton<T>._Instance == null) 12 { 13 Singleton<T>._Instance = Activator.CreateInstance<T>(); 14 } 15 return Singleton<T>._Instance; 16 } 17 } 18 19 static Singleton() 20 { 21 Singleton<T>._Instance = Activator.CreateInstance<T>(); 22 } 23 24 public static void CreateInstance() 25 { 26 if (Singleton<T>._Instance == null) 27 { 28 Singleton<T>._Instance = Activator.CreateInstance<T>(); 29 } 30 } 31 32 public static void DestroyInstance() 33 { 34 if (Singleton<T>._Instance != null) 35 { 36 Singleton<T>._Instance = (T)((object)null); 37 } 38 } 39 40 public static T GetInstance() 41 { 42 if (Singleton<T>._Instance == null) 43 { 44 Singleton<T>._Instance = Activator.CreateInstance<T>(); 45 } 46 return Singleton<T>._Instance; 47 } 48 }
时间: 2024-11-07 15:23:42