public class CacheHelper
{
private static Dictionary<string, object> dic = new Dictionary<string, object>();
// 定义一个静态变量来保存类的实例
private static CacheHelper session;
// 定义一个标识确保线程同步
private static readonly object locker = new object();
/// <summary>
/// 单例
/// </summary>
/// <returns>返回类型为Session</returns>
public CacheHelper Instance
{
get
{
if (session == null)
{
lock (locker)
{
if (session == null)// 如果类的实例不存在则创建,否则直接返回
{
session = new CacheHelper();
}
}
}
return session;
}
}
/// <summary>
/// 删除成员
/// </summary>
/// <param name="name"></param>
public void Remove(string name)
{
dic.Remove(name);
}
/// <summary>
/// 删除全部成员
/// </summary>
public void RemoveAll()
{
dic.Clear();
}
/// <summary>
/// 本类的索引器
/// </summary>
/// <returns>返回Object成员</returns>
public Object this[string index]
{
get
{
if (dic.ContainsKey(index))
{
Object obj = (Object)dic[index];
return obj;
}
return null;
}
set
{
dic.Add(index, value);
}
}
}