音频内容
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace AibisDream.Kit
|
||||
{
|
||||
public abstract class SingletonBase<T> : ISingleton where T : SingletonBase<T>
|
||||
{
|
||||
private static T _instance;
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance != null) return _instance;
|
||||
// 先获取所有非public的构造方法
|
||||
var ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
// 从ctors中获取无参的构造方法
|
||||
var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
|
||||
if (ctor == null)
|
||||
throw new Exception("Non-public ctor() not found!");
|
||||
// 调用构造方法
|
||||
_instance = ctor.Invoke(null) as T;
|
||||
_instance?.OnSingletonInit();
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void OnSingletonInit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user