我目前正在研究一个通用集合类,我想创建一个从集合中返回一个对象的方法。如果集合中不存在特定对象,则应创建该对象,将其添加到集合中,然后返回。
不过我遇到了一些问题。泛型集合如果是代表抽象类的类型,我在实例化它时遇到问题。
这是我的类定义:
public class CacheCollection<T> : List<CacheItem<T>> where T : EntityBase
这是我正在研究的部分完整的方法:
public T Item(int Id)
{
CacheItem<T> result = this.Where(t => t.Entity.Id == Id).First();
if (result == null) //item not yet in cache, load it!
{
T entity = new T(Id); //design time exception here: Cannot create an instance of the variable type 'T' because it does not have the new() constraint
result = new CacheItem<T>(entity);
this.Add(result);
}
return result.Entity;
}
关于如何解决这个问题有什么想法吗?
编辑:从 EntityBase 派生的所有类都将 Id 作为只读属性。
最佳答案
更新 2:嗯,您在评论中说您没有定义非泛型 CacheCollection
类型;但是你接着说你有一个 Dictionary<Type, CacheCollection>
.这些陈述不可能都是真的,所以我猜测 CacheCollection
你是说 CacheCollection<EntityBase>
.
现在问题来了:一个X<Derived>
不能转换为 X<Base>
如果X<T>
输入 is not covariant .也就是说,在你的情况下,只是因为 T
源自 EntityBase
并不意味着 CacheCollection<T>
源自 CacheCollection<EntityBase>
.
要具体说明这是为什么,请考虑 List<T>
类型。假设你有一个 List<string>
和一个 List<object>
. string
源自 object
, 但它不符合 List<string>
源自 List<object>
;如果是这样,那么你可以有这样的代码:
var strings = new List<string>();
// If this cast were possible...
var objects = (List<object>)strings;
// ...crap! then you could add a DateTime to a List<string>!
objects.Add(new DateTime(2010, 8, 23));
幸运的是,(在我看来)解决这个问题的方法非常简单。基本上,通过定义一个非泛型基类来遵循我最初的建议,CacheCollection<T>
将派生。更好的是,使用简单的非通用接口(interface)。
interface ICacheCollection
{
EntityBase Item(int id);
}
(请查看下面我更新的代码,了解如何在泛型类型中实现此接口(interface))。
那么对于你的字典,而不是 Dictionary<Type, CacheCollection<EntityBase>>
, 将其定义为 Dictionary<Type, ICacheCollection>
其余的代码应该放在一起。
更新:您似乎对我们隐瞒了!所以你有一个非通用的 CacheCollection
CacheCollection<T>
的基类推导,我说得对吗?
如果我对您对此答案的最新评论的理解是正确的,那么这是我给您的建议。编写一个类以提供对此 Dictionary<Type, CacheCollection>
的间接访问你的。这样你可以有很多CacheCollection<T>
在不牺牲类型安全的情况下实例。
类似这样的事情(注意:代码根据上面的新更新修改):
class GeneralCache
{
private Dictionary<Type, ICacheCollection> _collections;
public GeneralCache()
{
_collections = new Dictionary<Type, ICacheCollection>();
}
public T GetOrAddItem<T>(int id, Func<int, T> factory) where T : EntityBase
{
Type t = typeof(T);
ICacheCollection collection;
if (!_collections.TryGetValue(t, out collection))
{
collection = _collections[t] = new CacheCollection<T>(factory);
}
CacheCollection<T> stronglyTyped = (CacheCollection<T>)collection;
return stronglyTyped.Item(id);
}
}
这将允许您编写如下代码:
var cache = new GeneralCache();
RedEntity red = cache.GetOrAddItem<RedEntity>(1, id => new RedEntity(id));
BlueEntity blue = cache.GetOrAddItem<BlueEntity>(2, id => new BlueEntity(id));
好吧,如果T
源自 EntityBase
但没有无参数构造函数,最好的办法是指定一个将生成 T
的工厂方法在您的 CacheCollection<T>
中使用适当的参数构造函数。
像这样(注意:代码根据上面的新更新修改):
public class CacheCollection<T> : List<CacheItem<T>>, ICacheCollection where T : EntityBase
{
private Func<int, T> _factory;
public CacheCollection(Func<int, T> factory)
{
_factory = factory;
}
// Here you can define the Item method to return a more specific type
// than is required by the ICacheCollection interface. This is accomplished
// by defining the interface explicitly below.
public T Item(int id)
{
// Note: use FirstOrDefault, as First will throw an exception
// if the item does not exist.
CacheItem<T> result = this.Where(t => t.Entity.Id == id)
.FirstOrDefault();
if (result == null) //item not yet in cache, load it!
{
T entity = _factory(id);
// Note: it looks like you forgot to instantiate your result variable
// in this case.
result = new CacheItem<T>(entity);
Add(result);
}
return result.Entity;
}
// Here you are explicitly implementing the ICacheCollection interface;
// this effectively hides the interface's signature for this method while
// exposing another signature with a more specific return type.
EntityBase ICacheCollection.Item(int id)
{
// This calls the public version of the method.
return Item(id);
}
}
我还建议,如果您的商品将具有唯一 ID,请使用 Dictionary<int, CacheItem<T>>
作为您的后备存储而不是 List<CacheItem<T>>
因为它会使您的项目查找 O(1) 而不是 O(N)。
(我还建议使用私有(private)成员来实现此类以保存集合本身,而不是直接从集合继承,因为使用继承会公开您可能想要隐藏的功能,例如 Add
、 Insert
等。)
关于c# - 实例化泛型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3550173/