一种干净的方法来检查列表中对象的属性是否已更改? [英] A clean way to check if a property of an object in a list has been changed?

查看:42
本文介绍了一种干净的方法来检查列表中对象的属性是否已更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Codeproject,



我有一个名为UIControl的类,我还创建了像UIControlButton,UIControlImageBox这样的派生类。我已将所有这些存储在List< uicontrol>中。



有没有办法检查是否对List中的对象进行了任何更改只是添加或删除了一个对象?



我真的想确保没有更清晰的方法来检查是否进行了任何更改而不是每次比较所有属性它们设置在每个单独的类中。



简而言之,如何通过不使用object.Equal方法来确定对象的属性是否已更改这将比较一个对象是否在同一个内存位置 - 并且还使用一个不需要脏方法的方法,比如在对象本身内直接检查每个方法的所有属性。

< br $>
最好的问候,

- 编辑。



我尝试了什么:



我尝试通过比较一个列表到另一个列表来做一个脏方法,一个包含对象虽然没有改变,但可能会改变,比较那些物品,如果它们不同,将旧物品设置为新物品并触发如此事件:



 public bool IsIdentical(UIControl Control,UIControl Other)
{
if(Control.GetType()!= Other.GetType())
{
返回false;
}

if(!Control.Name.Equals(Other.Name))
{
return false;
}

返回false;
}





然而问题是某些派生控件有其他或多个值,我不知道t认为为每个不同的控件制作100个重载是一个好主意。

解决方案

你可能想要探索 INotifyPropertyChanged Interface(System.ComponentModel) [ ^ ]


< blockquote>大多数时候,你可以处理一个特定于控件类型的事件。 Windows中的文本框有 TextChanged 事件,列表框有 SelectionChanged


Hey Codeproject,

I have a class called UIControl, and I've also made derived class like UIControlButton, UIControlImageBox. I've stored all of these in a List<uicontrol>.

Is there any way to check if any changes have been made to the objects in the List that isn't just Added or Removed an object?

I really want to make sure there's no cleaner way to check if any changes have been made than to compare all properties each time they're set in each individual class.

So in a nutshell, how do I figure out if an object's properties have changed by not using the object.Equal method since that would compare whether or not an object is in the same memory location -- and also using a method that would not require a dirty method like checking all properties for each method directly within the object itself.

Best Regards,
- Eds.

What I have tried:

I've tried doing a dirty method by just comparing one list to another list, one containing the objects whilst not changed, and one that could possibly changed, comparing those objects and in case they're different setting the old item to the new item and trigger an event like so:

public bool IsIdentical(UIControl Control, UIControl Other)
{
    if(Control.GetType() != Other.GetType())
    {
        return false;
    }

    if(!Control.Name.Equals(Other.Name))
    {
        return false;
    }

    return false;
}



The problem however is that some derived controls have other or multiple values, and I don't think it's a great idea to make 100s of overloads for each different control.

解决方案

You may want to explore INotifyPropertyChanged Interface (System.ComponentModel)[^]


Most of the time, there's an event you can handle that is control type-specific. Text boxes in Windows have TextChanged events, list boxes have SelectionChanged, etc.


这篇关于一种干净的方法来检查列表中对象的属性是否已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆