单例类助手(代码)
时间:2010-11-22 来源:枢木
代码
/// <summary>
/// 单例类助手
/// </summary>
public sealed class SingletonHelper<T> where T : new()
{
private static T instance = new T();
private static object locker = new object();
private SingletonHelper() { }
/// <summary>
/// 获取单例
/// </summary>
/// <returns></returns>
public static T GetInstance()
{
if (null == instance)
{
lock (locker)
{
if (null == instance)
{
instance = new T();
}
}
}
return instance;
}
/// <summary>
/// 设置单例
/// </summary>
/// <param name="value"></param>
public void SetInstance(T value)
{
instance = value;
}
}
相关阅读 更多 +