TSingleton.cs 711 B

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. namespace XGame.Framework
  3. {
  4. public class TSingleton<TType> where TType : TSingleton<TType>, new()
  5. {
  6. private static TType _instance;
  7. public static void Dispose()
  8. {
  9. _instance?.OnDispose();
  10. _instance = null;
  11. }
  12. public static TType Instance
  13. {
  14. get
  15. {
  16. if (_instance == null)
  17. {
  18. _instance = Activator.CreateInstance<TType>();
  19. _instance.OnInit();
  20. }
  21. return _instance;
  22. }
  23. }
  24. protected virtual void OnInit() { }
  25. protected virtual void OnDispose() { }
  26. }
  27. }