如何检查对象是否改变? [英] How to check if an object has changed?
问题描述
我有一个对象,其中包含其它的目的,其中含有其它的目的,包括列表,等等。这个对象数据绑定到一个形式,揭露众多领域在不同的选项卡用户。
我也用主的孩子datagridviews。
I have an object, which contains other objects, which contain other objects, including lists, etcetera. This object is databound to a form, exposing numerous fields to the user in different tabs. I also use master-child datagridviews.
不知道如何检查,如果任何事情对象已改变相对于先前的时刻吗?无(手动)加入改变的变量,它被设置为true在所有(> 100)set方法。
Any idea how to check if anything has changed in this object with respect to an earlier moment? Without (manually) adding a changed variable, which is set to true in all (>100) set methods.
推荐答案
怎么办你定义的平等(介于新旧状态)?
How do you define "equality" (between old and new state)?
- 您比较的属性只或字段呢?
- 您只比较公开属性/字段?
- 请你曾经的忽略的任何属性/字段(即他们的修改并不重要)?
- 你如何比较原子类型(例如都是字符串比较不区分大小写的,或者你需要区分大小写的,有些地方为好)。
- Are you comparing properties only or fields as well?
- Are you only comparing public properties/fields?
- Do you ever ignore any properties/fields (i.e. their modifications do not matter)?
- How do you compare "atomic" types (e.g. are all string comparisons case-insensitive, or you need case-sensitive in some places as well).
如果这些问题的答案足够一般(即你可以设计出适用于所有对象的一组规则),那么你理论上可以达到你想要的东西通过的反射:基本思路是读取所有属性/根对象的字段,然后存放原子的人,并递归陷入了非原子那些(和重复整个过程)。后来,当您要检查如果有什么变化,你会重复递归下降,并与存储的值的比较结果。
If answers to these questions are general enough (i.e. you can devise a set of rules that apply to all of your objects), then you could theoretically accomplish what you want through reflection: The basic idea is to read all properties/fields of the "root" object, then store the "atomic" ones and recursively descend into the "non-atomic" ones (and repeat the whole process). Later, when you want to check if anything changed, you would repeat the recursive descent and compare the results with the stored values.
的我不主张这种方案特别高性能的,甚至容易(你需要制定一个健壮的命名约定用于存储旧值和大约多线程很小心),但它可能是一般。的
这篇关于如何检查对象是否改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!