Files
better-genshin-impact/BetterGenshinImpact/Model/Singleton.cs
2024-05-28 19:08:28 +08:00

28 lines
715 B
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Threading;
namespace BetterGenshinImpact.Model;
/// <summary>
/// 由于 C# 的 DI 过于难用bgi代码中依旧存在使用大量原始单例的对象
/// 给他们实现一个通用的单例模式
/// </summary>
/// <typeparam name="T"></typeparam>
public class Singleton<T> where T : class
{
protected static T? _instance;
protected static object? syncRoot;
public static T Instance => LazyInitializer.EnsureInitialized(ref _instance, ref syncRoot, CreateInstance);
protected static T CreateInstance()
{
return (T)Activator.CreateInstance(typeof(T), true)!;
}
public static void DestroyInstance()
{
_instance = null;
}
}