gpt4 book ai didi

c# - 字典 TryGetValue NullReferenceException

转载 作者:太空狗 更新时间:2023-10-30 00:33:01 25 4
gpt4 key购买 nike

<分区>

我在 _dicCache.TryGetValue(objID, out newObject); 处收到 NullReferenceException线。我完全不知道为什么会这样。请给我指出正确的方向好吗?

这是我的类(class):

public class Cache<T>
{
public string Name { get; set; }

private Dictionary<int, T> _dicCache = new Dictionary<int, T>();

public void Insert(int objID, T obj)
{
try
{
_dicCache.Add(objID, obj);

HttpContext.Current.Cache.Insert(Name, _dicCache, null, DateTime.Now.AddMinutes(10), TimeSpan.FromMinutes(0));
}
catch (Exception)
{
throw;
}
}

public bool Get(int objID, out T obj)
{
_dicCache = (Dictionary<int, T>)HttpContext.Current.Cache.Get(Name);


try
{
return _dicCache.TryGetValue(objID, out obj);
}
catch (Exception)
{
throw;
}
}
}

我是这样调用它的:

   Services.Cache<Entities.User> cache = new Services.Cache<Entities.User>();
cache.Name = Enum.Cache.Names.usercache.ToString();


Entities.User user = new Entities.User();

cache.Get(pUserId, out user);

我还尝试将 Get class 更改为:

    public T Get(int objID, out T obj)
{
_dicCache = (Dictionary<int, T>)HttpContext.Current.Cache.Get(Name);

T newObject = (T)Activator.CreateInstance<T>();


try
{
_dicCache.TryGetValue(objID, out newObject);

obj = newObject;

return obj;
}
catch (Exception)
{
throw;
}
}

但我仍然在 _dicCache.TryGetValue(objID, out newObject); 行得到相同的 NullReferenceException。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com