开发者

Strategy to keep csv string and Dictionary<string,string> synchronized at all times

If I have a comma separated list string property:

"age:123,name:john,last:doe"

And another property of Dictionary type.

How could I keep both of these properties synchronized 开发者_如何学Pythonsuch that added to either one will update the other.

example:

obj.CsvString += "address:123 drive";

or

obj.SomeDic.Add("address", "123 drive");

How could I do this? Should there be other methods to adding/removing?


Do you HAVE to do both?

I would only add to the dictionary and have a method that returns the dictionary as a string. No sync required then.


You'll need to implement your own class that either derives from Dictionary, or implements IDictionary. You can then overload (implement) your own add/update/remove methods that both stores your in-memory version (the dictionary) as well as the CSV.


You could implement or borrow an ObservableDictionary and use it as SomeDic. Then, in the changed event, change the backing field for your CSV. In the set_CsvString setter, you can replace the dictionary field with a new one based on the CSV.


You can create you own class that inherits from IList, then create an event that gets raised when you add an element.

You can then subscribe to the event and when ever it is raised you just update the other list/dictionary.

public class CustomList : IList<T>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜