Unity 单例模板类
unity 不继承Mono的单例模板
代码片段
public class BaseManager where T:BaseManager
{
///
/// 类型实例
///
private static T m_instance;
///
/// 双重锁,确保安全
///
private static readonly object lock_Obj = new object();
public static T Instance
{
get
{
lock (lock_Obj)
{
if (m_instance == null)
{
lock (lock_Obj)
{
var ctors = typeof(T).GetConstructors(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
if (ctor == null)
{
throw new Exception("Non_public ctor() not Found!!!");
}
m_instance = ctor.Invoke(null) as T;
}
}
}
return m_instance;
}
}
protect void T(){}
}