-
Notifications
You must be signed in to change notification settings - Fork 10
/
LazySingleton.cs
24 lines (21 loc) · 963 Bytes
/
LazySingleton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace yaSingleton {
/// <summary>
/// Singleton class. It'll be lazy-initialized when first accessed.
/// Inherit by passing the inherited type (e.g. class GameManager : LazySingleton<GameManager>)
/// </summary>
/// <typeparam name="TSingleton">The Inherited Singleton's Type</typeparam>
public abstract class LazySingleton<TSingleton> : BaseSingleton where TSingleton : BaseSingleton {
public static TSingleton Instance {
get { return Initializer<TSingleton>.LazyInstance; }
}
/// <summary>
/// No need to manually create instances of LazySingletons
/// </summary>
internal override void CreateInstance() { }
// ReSharper disable once ClassNeverInstantiated.Local
private class Initializer<T> where T : BaseSingleton {
static Initializer() { }
internal static readonly T LazyInstance = GetOrCreate<T>();
}
}
}