繁体   English   中英

支持引用类型和值类型

[英]Support for both reference types and value types

我已经用这种方法来获得此类,在这种方法中,我需要同时支持引用类型和值类型,但是我不知道该怎么执行。 任何帮助将非常感激。

public static class CacheHelper<T>
{
    public static T Get(string key, Func<T> function) {
        var obj = (T)HttpContext.Current.Cache[key];
        if (obj == null)
        {
            obj = function.Invoke();
            HttpContext.Current.Cache.Add(key, obj, null, DateTime.Now.AddMinutes(3),
                TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
        }
        return (T)obj;
    }
}

我认为,如果你删除投地T在这条线var obj = HttpContext.Current.Cache[key]; 你应该没事的。

public static class CacheHelper<T>
{
    public static T Get(string key, Func<T> function) {
        var obj = HttpContext.Current.Cache[key];
        if (obj == null)
        {
            obj = function.Invoke();
            HttpContext.Current.Cache.Add(key, obj, null, DateTime.Now.AddMinutes(3),
                TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
        }
        return (T)obj;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM