检测是否包含在字典中的值发生变化 [英] Detecting if a value contained in a Dictionary has changed
问题描述
如果我得到从IDictionary的一类,我怎么能肯定,之后它被添加到底层集合任何给定的V有没有变化?
If I derive a class from IDictionary, how can I be certain that any given V has not changed after it is added to the underlying collection?
推荐答案
有很多方法,但要看...
There's many ways, but it depends...
如果添加到字典中的值类型包含了一个改变事件类型,订阅它。
If the value type added to the dictionary contains a "Changed" type of event, subscribe to it.
如果值类型不包含这样的事件,但你控制的来源$ C $ C,添加事件,并订阅了。
If the value type doesn't contain such an event, but you control the source code, add the event and subscribe to it.
如果值类型不包含这样一个事件,你不控制源头code,考虑维护的值的卷影副本与克隆的价值或什么的,并做比较。
If the value type doesn't contain such an event, and you don't control the source code, consider maintaining a shadow copy of the values with cloned values or something, and do comparisons.
我怕在最后一种情况出现,在那里你不用管的类型,它不包含事件,你别无选择,而是以某种方式使每个值的当前状态的记录,后来就比较的国家,看它是否已经改变了。
I'm afraid that in the last case there, where you don't control the type and it doesn't contain events, you're left with no choice but to somehow make a record of the current state of each value, and later on compare to that state to see if it has changed.
这篇关于检测是否包含在字典中的值发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!