Reputation:
I have an object with five properties and each of these properties has two states ("before" and "after").
How do I get the information about which properties changed their state?
The only way that I am familiar with is to get a list of all the properties (using Reflection?), then use a loop to compare each property between two objects and store information about those which were changed.
Is there a simple way to do it, perhaps using LINQ?
Upvotes: 0
Views: 2634
Reputation: 840
You can do something like this:
public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);
public class PropertyChangedEventArgs : EventArgs
{
public PropertyChangedEventArgs(string propertyName, dynamic oldValue, dynamic newValue)
{
this.PropertyName = propertyName;
this.OldValue = oldValue;
this.NewValue = newValue;
}
public virtual string PropertyName { get; private set; }
public virtual dynamic OldValue { get; private set; }
public virtual dynamic NewValue { get; private set; }
}
public class PropertyClass
{
public event PropertyChangedEventHandler PropertyChanged;
private void Set<T>(string propertyName, ref T field, T value)
{
if (field.Equals(value))
return;
T oldValue = value;
field = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName, oldValue, value));
}
// Properties
private string _name;
private string _message;
private bool _isMember;
public string Name
{
get { return _name; }
set { Set("Name", ref _name, value); }
}
public string Message
{
get { return _message; }
set { Set("Message", ref _message, value); }
}
public bool IsMember
{
get { return _isMember; }
set { Set("IsMember", ref _isMember, value); }
}
}
Upvotes: 1
Reputation: 10347
The interface INotifyProprtyChanged
requires you to implement an event PropertyChanged
. You can subscribe to this interface in the class itself and track properties for which they get called.
For example:
internal class SampleClass : INotifyPropertyChanged{
public event PropertyChangedEventHandler PropertyChanged;
private string _SampleProperty;
internal List<string> _ChangedProperties;
public SampleClass() {
this.PropertyChanged += SampleClass_PropertyChanged;
_ChangedProperties = new List<string>();
}
protected virtual void OnPropertyChanged( string propertyName ) {
PropertyChangedEventHandler handler = PropertyChanged;
if ( handler != null )
handler( this, new PropertyChangedEventArgs( propertyName ) );
}
void SampleClass_PropertyChanged( object sender, PropertyChangedEventArgs e ) {
if ( _ChangedProperties.Contains( e.PropertyName ) ) return;
_ChangedProperties.Add( e.PropertyName );
}
public string SampleProperty {
get { return _SampleProperty; }
set {
if (_SampleProperty == value )
return;
_SampleProperty = value;
OnPropertyChanged( "SampleProperty" );
}
}
}
Now you have a list of changed properties. You can work further by remembering values, etc.
I have not considered thread safety, I would not consider this sample production ready.
Upvotes: 1