2022-11-10 00:30:01 +08:00

56 lines
1.5 KiB
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.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Milimoe.FunGame.Core.Api.Utility
{
/// <summary>
/// 单例表表中的对象以类名作为Key保存并以Key获取该对象Key具有唯一约束
/// 用于储存单例对象使用
/// </summary>
public class Singleton
{
private static readonly Hashtable SingletonTable = new();
public static bool Add(object single)
{
string type = single.GetType().ToString();
if (!SingletonTable.ContainsKey(type))
{
try
{
SingletonTable.Add(type, single);
}
catch
{
throw new Exception("添加单例到单例表时遇到错误");
}
return true;
}
return false;
}
public static T? Get<T>()
{
T? single = default;
string type = typeof(T).ToString();
if (SingletonTable.ContainsKey(type))
{
try
{
single = (T?)SingletonTable[type];
}
catch
{
throw new Exception("不能从单例表中获取到指定的单例");
}
if (single != null) return single;
}
return single;
}
}
}