我创建了一个 ConcurrentDictionary 但不确定如何更新它的元素:
public class ModelClient : ICloneable
{
public Session session;
public List<string> keys = new List<string>();
public List<ModelOrder> orders = new List<ModelOrder>();
public ModelClient(SessionID sessionID)
{
session = Session.LookupSession(sessionID);
}
public object Clone() { return this.MemberwiseClone(); }
}
public class ModelOrder : ICloneable
{
private string Symbol;
private int Amount;
private double Price;
public ModelOrder(string Symbol, int Amount, double Price)
{
this.Symbol = Symbol;
this.Amount = Amount;
this.Price = Price;
}
public object Clone() { return this.MemberwiseClone(); }
}
public ConcurrentDictionary<SessionID, ModelClient> ModelClients = new ConcurrentDictionary<SessionID, ModelClient>();
这是我需要更新的地方(通过向订单添加新元素),看来我应该使用 TryUpdate,但它看起来非常麻烦且难以阅读。
有人可以告诉我一个好方法吗。
public bool AddModelClientOrder(SessionID sessionID, string orderMessage)
{
string[] part = orderMessage.Split(' ');
//HOW DO I BEST DO THIS?
ModelClients[sessionID].orders.Add(new ModelOrder(part[0], int.Parse(part[1]), double.Parse(part[2])));
}