在继承自 List<T> 的 Class 中更新此项
Updating this in Class that inherits from List<T>
我有一个用于缓存的抽象class实现如下(简化)
public abstract class DataCacheMember<T> : List<T>
{
private List<T> _data;
public List<T> Data
{
get
{
if (_data == null || _data.Count() < 1)
_data = GetData();
return _data;
}
}
private string ApiEndPoint {get; set;}
private Timer timer;
private List<T> GetData()
{
//call api and get data
}
private void RefreshData()
{
_data = GetData();
}
protected DataCacheMember(string apiEndPoint)
{
ApiEndPoint = apiEndPoint;
timer = new System.Threading.Timer(
e => RefreshData(),
null,
TimeSpan.Zero,
TimeSpan.FromMinutes(10));
}
}
它允许 rapid 使用一个简单的字符串为 api 端点创建缓存对象:
public class StateMap<Properties> : DataCacheMember<Properties>
{
public StateMap(string apiEndPoint = "Property/GetProperties")
: base(apiEndPoint)
{}
}
从 List<T>
继承的全部原因是为了消除对字段的需求。
但是,如果我尝试修改构造函数并刷新为:
private void RefreshData()
{
this = GetData() as DataCacheMember<T>;
}
protected DataCacheMember(string apiEndPoint)
{
this = GetData() as DataCacheMember<T>;
}
我收到错误 Cannot assign to <this> because it is Read Only
。
解决这个问题的正确方法是什么?我只需要使用 Clear()
和 AddRange()
来管理对象吗?
如果我这样做,我发现第一次调用该对象将 return 为空,因为该对象可以 return 在构造函数完成它的调用之前。
要回答不能在构造函数或任何其他方法中分配 this
的问题。您可以添加从 GetData()
:
返回的项目
private void RefreshData()
{
this.Clear();
this.AddRange(GetData());
}
protected DataCacheMember(string apiEndPoint)
{
this.Clear();
this.AddRange(GetData());
}
但是继承形式List<T>
可能不是这里的正确设计。
根据Using this() in C# Constructors
private void RefreshData()
{
this = GetData() as DataCacheMember<T>;
}
protected DataCacheMember(string apiEndPoint)
{
this = GetData() as DataCacheMember<T>;
}
这些只能在结构中工作,并没有真正做任何有用的事情,而且是糟糕的设计。
我有一个用于缓存的抽象class实现如下(简化)
public abstract class DataCacheMember<T> : List<T>
{
private List<T> _data;
public List<T> Data
{
get
{
if (_data == null || _data.Count() < 1)
_data = GetData();
return _data;
}
}
private string ApiEndPoint {get; set;}
private Timer timer;
private List<T> GetData()
{
//call api and get data
}
private void RefreshData()
{
_data = GetData();
}
protected DataCacheMember(string apiEndPoint)
{
ApiEndPoint = apiEndPoint;
timer = new System.Threading.Timer(
e => RefreshData(),
null,
TimeSpan.Zero,
TimeSpan.FromMinutes(10));
}
}
它允许 rapid 使用一个简单的字符串为 api 端点创建缓存对象:
public class StateMap<Properties> : DataCacheMember<Properties>
{
public StateMap(string apiEndPoint = "Property/GetProperties")
: base(apiEndPoint)
{}
}
从 List<T>
继承的全部原因是为了消除对字段的需求。
但是,如果我尝试修改构造函数并刷新为:
private void RefreshData()
{
this = GetData() as DataCacheMember<T>;
}
protected DataCacheMember(string apiEndPoint)
{
this = GetData() as DataCacheMember<T>;
}
我收到错误 Cannot assign to <this> because it is Read Only
。
解决这个问题的正确方法是什么?我只需要使用 Clear()
和 AddRange()
来管理对象吗?
如果我这样做,我发现第一次调用该对象将 return 为空,因为该对象可以 return 在构造函数完成它的调用之前。
要回答不能在构造函数或任何其他方法中分配 this
的问题。您可以添加从 GetData()
:
private void RefreshData()
{
this.Clear();
this.AddRange(GetData());
}
protected DataCacheMember(string apiEndPoint)
{
this.Clear();
this.AddRange(GetData());
}
但是继承形式List<T>
可能不是这里的正确设计。
根据Using this() in C# Constructors
private void RefreshData()
{
this = GetData() as DataCacheMember<T>;
}
protected DataCacheMember(string apiEndPoint)
{
this = GetData() as DataCacheMember<T>;
}
这些只能在结构中工作,并没有真正做任何有用的事情,而且是糟糕的设计。