123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System;
- using System.Threading;
- using Fort23.UTool;
- namespace Utility
- {
- public class Singleton<T> : IDisposable where T : new()
- {
- static object m_lock = new object();
- #if COMBAT_SERVER
- private static Map<int, T> _threadingInstance = new Map<int, T>();
- #endif
- #if !COMBAT_SERVER
- protected static T m_instance;
- public static T Instance
- {
- get
- {
-
- if (m_instance == null)
- {
- lock (m_lock)
- {
- if (m_instance == null)
- {
- m_instance = new T();
- }
- }
- }
- return m_instance;
- }
- set { m_instance = value; }
- }
- public virtual void Dispose()
- {
-
- ProDispose();
- Instance = (T) (object) null;
- }
- #else
- public static T Instance
- {
- get
- {
- int id = Thread.CurrentThread.ManagedThreadId;
-
- if (_threadingInstance.TryGetValue(id, out T VALUE))
- {
- return VALUE;
- }
- lock (_threadingInstance)
- {
- if (_threadingInstance.TryGetValue(id, out T VALUE2))
- {
- return VALUE2;
- }
- else
- {
- // LogTool.Log("新建示例"+id+"__"+typeof(T));
- T iInstance = new T();
- _threadingInstance.Add(id, iInstance);
- return iInstance;
- }
- }
- }
- }
- public virtual void Dispose()
- {
- ProDispose();
- lock (_threadingInstance)
- {
- int id = Thread.CurrentThread.ManagedThreadId;
- _threadingInstance.Remove(id);
- }
- }
- #endif
- protected virtual void ProDispose()
- {
- }
- }
- }
|