我有一个通用的单例模式,看起来像这样:
错误信息为:
public class Cache<T>
{
private Dictionary<Guid, T> cachedBlocks;
// Constructors and stuff, to mention this is a singleton
public T GetCache(Guid id)
{
if (!cachedBlocks.ContainsKey(id))
cachedBlocks.Add(id, LoadFromSharePoint(id))
return cachedBlocks[id];
}
public T LoadFromSharePoint(Guid id)
{
return new T(id) // Here is the problem.
}
}
错误信息为:
我必须提到,我必须传递该 id 参数,没有其他方法可以这样做。有什么解决方法吗?非常感谢。无法创建类型 T 的实例,因为它没有 new() 约束。