无法隐式将类型“T”转换为“T”。

3
我正在为一个缓存提供程序编写流畅的注册,但不知何故我的泛型并不理想。我在这一部分遇到了错误:value = _loadFunction();
Cannot implicitly convert type 'T' to 'T [HttpRuntimeCache.cs(10)]

以下是代码:

using IDM.CMS3.Service.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace IDM.CMS3.Web.Public.CacheProviders
{
    public class HttpRuntimeCache<T> : IFluentCacheProvider<T>
    {
        string _key;
        Func<T> _loadFunction;
        DateTime? _absoluteExpiry;
        TimeSpan? _relativeExpiry;

        public HttpRuntimeCache()
        {

        }

        public IFluentCacheProvider<T> Key(string key)
        {
            _key = key;
            return this;
        }

        public IFluentCacheProvider<T> Load(Func<T> loadFunction)
        {
            _loadFunction = loadFunction;
            return this;
        }

        public IFluentCacheProvider<T> AbsoluteExpiry(DateTime absoluteExpiry)
        {
            _absoluteExpiry = absoluteExpiry;
            return this;
        }

        public IFluentCacheProvider<T> RelativeExpiry(TimeSpan relativeExpiry)
        {
            _relativeExpiry = relativeExpiry;
            return this;
        }

        public T Value()
        {
            return FetchAndCache<T>();
        }

        public void InvalidateCacheItem(string cacheKey)
        {
            throw new NotImplementedException();
        }

        T FetchAndCache<T>()
        {
            T value;
            if (!TryGetValue<T>(_key, out value))
            {
                value = _loadFunction();
                if (!_absoluteExpiry.HasValue)
                    _absoluteExpiry = Cache.NoAbsoluteExpiration;

                if (!_relativeExpiry.HasValue)
                    _relativeExpiry = Cache.NoSlidingExpiration;

                HttpContext.Current.Cache.Insert(_key, value, null, _absoluteExpiry.Value, _relativeExpiry.Value);

            }
            return value;
        }

        bool TryGetValue<T>(string key, out T value)
        {
            object cachedValue = HttpContext.Current.Cache.Get(key);
            if (cachedValue == null)
            {
                value = default(T);
                return false;
            }
            else
            {
                try
                {
                    value = (T)cachedValue;
                    return true;
                }
                catch
                {
                    value = default(T);
                    return false;
                }
            }
        }
    }
}

1
你正在使用与 HttpRuntimeCache<T> 类声明中相同名称的类型参数来遮蔽 FetchAndCache<T> 中的一个。这些是不同的 T。看起来你并不真正需要在方法上重复类型参数。 - millimoose
1
T FetchAndCache<T>bool TryGetValue<T> 正在重新定义一个新的 T 类型。我认为,如果删除额外的泛型声明,它应该能正常工作。也就是说,将它们改写成 T FetchAndCache()bool TryGetValue(...) 就可以了。 - Chris Sinclair
1
没错,就是这样。我甚至看到了绿色的波浪线。如果你们发布答案,我会给你们信用。 - Jason More
1
你应该已经收到了编译器的警告。你是(1)收到了编译器的警告但忽略了它,还是(2)没有收到警告?如果是(1),那么开始注意编译器的警告。如果是(2),如果你发布一个最小化的复现代码,我会将这个错误传递给编译器团队。 - Eric Lippert
是的,但我发现我的警告未启用。 :-/ - Jason More
显示剩余3条评论
1个回答

5
< p > T FetchAndCache<T>bool TryGetValue<T> 正在重新定义一个与在类级别声明的T类型不同的新的 T类型。我认为,一旦删除额外的泛型声明,它应该可以正常工作。也就是说,将它们重写为:

T FetchAndCache()
{
     ...
}

bool TryGetValue(string key, out T value)
{
     ...
}

一旦你这样做了,编译器会将这里的 T 识别为类上声明的那个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接