You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
3 years ago
|
namespace ET.Util
|
||
|
{
|
||
|
using System;
|
||
|
/// <summary>
|
||
|
/// A base class for the singleton design pattern.
|
||
|
/// https://www.codeproject.com/Articles/572263/A-Reusable-Base-Class-for-the-Singleton-Pattern-in
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T">Class type of the singleton</typeparam>
|
||
|
public abstract class SingletonBase<T> where T : class
|
||
|
{
|
||
|
#region Members
|
||
|
|
||
|
/// <summary>
|
||
|
/// Static instance. Needs to use lambda expression
|
||
|
/// to construct an instance (since constructor is private).
|
||
|
/// </summary>
|
||
|
private static readonly Lazy<T> sInstance = new Lazy<T>(() => CreateInstanceOfT());
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Properties
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the instance of this singleton.
|
||
|
/// </summary>
|
||
|
public static T Instance { get { return sInstance.Value; } }
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Methods
|
||
|
|
||
|
/// <summary>
|
||
|
/// Creates an instance of T via reflection since T's constructor is expected to be private.
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
private static T CreateInstanceOfT()
|
||
|
{
|
||
|
return Activator.CreateInstance(typeof(T)) as T;
|
||
|
//return new T();
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
}
|