Consider the following object:
Public Class Foo
dim m_SomePropertyInFoo as string
public property SomePropertyInFoo as string
get
return m_SomePropertyInFoo
end get
set(value as string)
m_SomePropertyInFoo = value
end set
end Class
I want to determine when SomePropertyInFoo has a new value which would qualify the instance of Foo as dirty. Here is the catch: I don’t want to have to call some function in each setter because I don’t want the devs to have to implement code each time(because they could make a mistake and miss a property).
There are two options I could see.
If your type implements
INotifyPropertyChanged, you could subscribe to the object’sPropertyChangedevent and use this to track for changes.Otherwise, if you want to implement this with no changes to the code of the properties, external change tracking would be required. You could make a method that used Reflection (or potentially PropertyDescriptors) to grab the value of each property, and later, compare it to the current values to determine if that object is “dirty”. This is not an inexpensive operation, however.
Another, potentially more elegant, option would be to use Aspect Oriented Programming to add the code to each property automatically at compile time. PostSharp could be made to handle this fairly easily, for example.