Skip to content

Instantly share code, notes, and snippets.

@RedRoserade
Last active August 29, 2015 14:27
Show Gist options
  • Save RedRoserade/de0183b15e472e5e17d2 to your computer and use it in GitHub Desktop.
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.
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