C# 创建通用列表<;T>;在基类中,并在具有不同T
我从中派生了基类和管理器类:C# 创建通用列表<;T>;在基类中,并在具有不同T,c#,generics,C#,Generics,我从中派生了基类和管理器类: public class CBase<TC> where TC : class, new() { protected CBase() {} protected static ConcurrentDictionary<object, Lazy<TC>> _instances = new ConcurrentDictionary<object, Lazy<TC>>();
public class CBase<TC> where TC : class, new()
{
protected CBase() {}
protected static ConcurrentDictionary<object, Lazy<TC>> _instances = new ConcurrentDictionary<object, Lazy<TC>>();
public static TC GetInstance(object key)
{
return _instances.GetOrAdd(key, k => new Lazy<TC>(() => new TC())).Value;
}
}
public class CSeriesManager : CBase<CSeriesManager>
{
private List<CSeries.SSeries> _items = null;
public List<CSeries.SSeries> Series
{
get
{
if (_items == null) _items = new List<CSeries.SSeries>();
return _items;
}
}
}
公共类CBase,其中TC:class,new()
{
受保护的CBase(){}
受保护的静态ConcurrentDictionary _实例=新ConcurrentDictionary();
公共静态TC GetInstance(对象键)
{
返回_instances.GetOrAdd(key,k=>newlazy(()=>newtc()).Value;
}
}
公共类CSeriesManager:CBase
{
私有列表_items=null;
公开名单系列
{
得到
{
如果(_items==null)_items=new List();
退货(物品);;
}
}
}
我将有几个管理器类,每个类都有类似于列表的字段,并在getter中检查NULL。是否可以使此字段成为泛型字段,并将其移动到基类而不进行额外的装箱/铸造
这就是我到目前为止所做的:
public class CBase<TC> where TC : class, new()
{
protected CBase() {}
protected List<object> _items = new List<object>();
protected static ConcurrentDictionary<object, Lazy<TC>> _instances = new ConcurrentDictionary<object, Lazy<TC>>();
public static TC GetInstance(object key)
{
return _instances.GetOrAdd(key, k => new Lazy<TC>(() => new TC())).Value;
}
public List<TL> GetItems<TL>()
{
return _items.ConvertAll(x => (TL)x);
}
}
公共类CBase,其中TC:class,new()
{
受保护的CBase(){}
受保护列表_items=新列表();
受保护的静态ConcurrentDictionary _实例=新ConcurrentDictionary();
公共静态TC GetInstance(对象键)
{
返回_instances.GetOrAdd(key,k=>newlazy(()=>newtc()).Value;
}
公共列表GetItems()
{
返回_items.ConvertAll(x=>(TL)x);
}
}
有没有人对如何改进/加速它有什么建议?这就是你想要的:
public class CBase<TC, LT> where TC : class, new()
{
protected CBase() {}
protected static ConcurrentDictionary<object, Lazy<TC>> _instances = new ConcurrentDictionary<object, Lazy<TC>>();
public static TC GetInstance(object key)
{
return _instances.GetOrAdd(key, k => new Lazy<TC>(() => new TC())).Value;
}
private List<LT> _items = null;
public List<LT> Series
{
get
{
if (_items == null) _items = new List<LT>();
return _items;
}
}
}
public class CSeriesManager : CBase<CSeriesManager, SSeries>
{
}
公共类CBase,其中TC:class,new()
{
受保护的CBase(){}
受保护的静态ConcurrentDictionary _实例=新ConcurrentDictionary();
公共静态TC GetInstance(对象键)
{
返回_instances.GetOrAdd(key,k=>newlazy(()=>newtc()).Value;
}
私有列表_items=null;
公开名单系列
{
得到
{
如果(_items==null)_items=new List();
退货(物品);;
}
}
}
公共类CSeriesManager:CBase
{
}
为什么不将类型参数TL
包含在CBase
的一般定义中?这似乎没有意义:CSeriesManager:CBase
。列表中的TL
从何而来?它是如何定义的?@PatrickHofman这里是方法的一个参数(方法可以是泛型的)@Art:如果你想让这些字段具有泛型行为,你必须这样做。是的,类似这样,但最好不要在CBase中仅限于2种类型,这样可以将多个类型传递到基类中吗?这正是我要写的:)@Art-你可以做任何你想做的事情。我相信这是有限度的,但我从来没有达到过。不过,我确实建议,如果您有许多通用参数,那么您的设计就是错误的。我想不出有多少次我会使用超过4个。@Enigmativity:好的,我同意,设计不好,所以我稍微修改了一下