Last active
August 29, 2015 14:27
-
-
Save RedRoserade/de0183b15e472e5e17d2 to your computer and use it in GitHub Desktop.
A class that offers a method that will merge the properties of the current object using a dictionary and raise propertychanged events for each item.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ObservableModel : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged = delegate { }; | |
public void Merge(IDictionary<string, object> updates) | |
{ | |
var selfType = GetType(); | |
foreach (var prop in updates) | |
{ | |
selfType.GetProperty(prop.Key).SetValue(this, prop.Value); | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop.Key)); | |
} | |
} | |
} | |
// Usage: | |
Model.Merge(new Dictionary<string, object> | |
{ | |
[nameof(Model.Property1)] = prop1, | |
[nameof(Model.Property2)] = prop2, | |
[nameof(Model.Property3)] = prop3 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment