1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- namespace ET
- {
- public interface ISingleton: IDisposable
- {
- void Register();
- void Destroy();
- bool IsDisposed();
- }
-
- public abstract class Singleton<T>: ISingleton where T: Singleton<T>, new()
- {
- private bool isDisposed;
- [StaticField]
- private static T instance;
- public static T Instance
- {
- get
- {
- return instance;
- }
- }
- void ISingleton.Register()
- {
- if (instance != null)
- {
- throw new Exception($"singleton register twice! {typeof (T).Name}");
- }
- instance = (T)this;
- }
- void ISingleton.Destroy()
- {
- if (this.isDisposed)
- {
- return;
- }
- this.isDisposed = true;
-
- instance.Dispose();
- instance = null;
- }
- bool ISingleton.IsDisposed()
- {
- return this.isDisposed;
- }
- public virtual void Dispose()
- {
- }
- }
- }
|