using System; using System.Collections.Generic; namespace IFramework.Singleton { /// /// 单例合集 /// public static class SingletonCollection { static Dictionary pairs; static SingletonCollection() { pairs = new Dictionary(); // Framework.onDispose += Dispose; } /// /// 注入单例 /// /// /// public static void Set(T singleton) where T : ISingleton { Type type = typeof(T); if (!pairs.ContainsKey(type)) pairs.Add(type, singleton); else throw new Exception("Singleton Err"); } /// /// 注销一个单例 /// /// public static void Dispose() where T : ISingleton { Type type = typeof(T); if (pairs.ContainsKey(type)) { pairs[type].Dispose(); pairs.Remove(type); } else throw new Exception("SingletonPool dispose Err "+typeof(T)); } /// /// 注销所有单例 /// public static void Dispose() { foreach (var item in pairs.Values) { item.Dispose(); } pairs.Clear(); } } }