ConfigSingleton.cs 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. namespace ET
  3. {
  4. public abstract class ConfigSingleton<T>: ProtoObject, ISingleton where T: ConfigSingleton<T>, new()
  5. {
  6. [StaticField]
  7. private static T instance;
  8. public static T Instance
  9. {
  10. get
  11. {
  12. return instance ??= ConfigComponent.Instance.LoadOneConfig(typeof (T)) as T;
  13. }
  14. }
  15. void ISingleton.Register()
  16. {
  17. if (instance != null)
  18. {
  19. throw new Exception($"singleton register twice! {typeof (T).Name}");
  20. }
  21. instance = (T)this;
  22. }
  23. void ISingleton.Destroy()
  24. {
  25. T t = instance;
  26. instance = null;
  27. t.Dispose();
  28. }
  29. bool ISingleton.IsDisposed()
  30. {
  31. throw new NotImplementedException();
  32. }
  33. public override void AfterEndInit()
  34. {
  35. }
  36. public virtual void Dispose()
  37. {
  38. }
  39. }
  40. }