using System;
namespace VCommon
{
///
/// 惰性初始化包裹, 线程安全
///
public class LazyHolder where T : class
{
private readonly object _lock = new object();
private readonly Func _initFunc;
private T _heldInstance;
public LazyHolder(Func initFunc) => _initFunc = initFunc;
public T Instance
{
get
{
if (_heldInstance == null)
{
lock (_lock)
{
if (_heldInstance == null)
{
_heldInstance = _initFunc();
}
}
}
return _heldInstance;
}
}
}
}