SingletonBaseT 类

线程安全、防反射、防序列化的单例抽象基类(.NET 6+ 版本)

使用 Lazy<T> 实现延迟初始化和线程安全;并可以重写OnInstanceCreated自定义初始化逻辑

Definition

命名空间: Suresking.Utility
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
C#
[SerializableAttribute]
public abstract class SingletonBase<T> : ISerializable
where T : SingletonBase<T>
Inheritance
Object    SingletonBaseT
Implements
ISerializable

类型参数

T
具体的单例类型,必须继承自 SingletonBase<T>

备注

使用示例: public sealed class MyService : SingletonBase<MyService> { private MyService() { } public void DoWork() => Console.WriteLine("Working..."); } // 使用 var service = MyService.Instance; service.DoWork();

构造函数

SingletonBaseT 受保护的默认构造函数,防止外部直接使用 new 创建实例
SingletonBaseT(SerializationInfo, StreamingContext) 反序列化专用的构造函数

属性

Instance 获取单例实例
IsInstanceCreated 检查单例是否已被创建

方法

GetObjectData 实现 ISerializable 接口的序列化方法
OnInstanceCreated 实例创建完成后的初始化钩子

参见