unity 不继承Mono的单例模板
代码片段
public class BaseManager<T> where T:BaseManager<T>
{
/// <summary>
/// 类型实例
/// </summary>
private static T m_instance;
/// <summary>
/// 双重锁,确保安全
/// </summary>
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(){}
}
|