12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System;
- using System.Collections.Generic;
- namespace IFramework.Singleton
- {
-
-
-
- public static class SingletonCollection
- {
- static Dictionary<Type, ISingleton> pairs;
- static SingletonCollection()
- {
- pairs = new Dictionary<Type, ISingleton>();
-
- }
-
-
-
-
-
- public static void Set<T>(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<T>() 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();
- }
- }
- }
|