我有基類和管理類從它派生的使用它:創建泛型列表<T>並在派生類中有不同的T
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;
}
}
}
我將有幾個管理類和他們每個人都會有類似於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);
}
}
有誰知道如何改進的建議/加速呢?
你爲什麼不只是包括類型參數'TL'成'CBase的'一般的定義是什麼? –
Carsten
這似乎沒有道理:'CSeriesManager:CBase'。 –
'List'中的'TL'從哪裏來的?它是如何定義的? –