次のような汎用シングルトンがあります。
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.
}
}
エラーメッセージは次のとおりです。
new() 制約がないため、タイプ T のインスタンスを作成できません。
そのパラメーターを渡す必要id
があり、それ以外の方法はありません。これを解決する方法についてのアイデアは大歓迎です。