Indian Troy

Indian Troy

  • NA
  • 1
  • 0

.net 2.0 INotifyPropertyChanged interface Implementation

Jul 27 2006 11:42 AM
Dear friends, In my current project I am using the CustomCollections with the help of BindingList generic class to store the database records instead using DataSet objects as offline database identities. Now I do have a large number of property classes each having a large number of properties in it. I want to implement the INotifyPropertyChanged interface in every property class as follows: The purpose to implement the INotifyPropertyChanged interface is I want to inform the UI whenever any property change occurs and update the it accodingly. Also I need to inform the ListChanged event of the my BindingList collection object using the PropertyChangedEvent of INotifyPropertyChanged interface. I know that the PropertyChangedEvent automatically raises the ListChanged event of the BindingList object. I want to maintain a flag whose value will be false whenever the collection gets changed means any property value of any item in the collection gets changed. My code is like below: public class Customer :INotifyPropertyChanged { private int number; public int Number { get { return number; } set { number = value;if(value!=number) OnPropertyChanged("Number"); } } private string name; public string Name { get { return name; } set { name = value;if(value != name) OnPropertyChanged("Name"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } Now what if my class has 100 properties then.... Is there any way out so I dont need to write the same code like below again and again in each property's set method: if(value != name) OnPropertyChanged("Name"); I dont want to write the above line for every property. So is it possible to do this using Reflection and subscribe for the event PropertyChanged for every property in the class in a single code block.. I have seen such a code months ago on any website but can't recall it right now as that time I was not so serious and not knowing I might have to implement this in future:) Suggest me how to do this and put the code snippet if possible. THANKS in ADVANCE. Regards, Hardeek Thakkar