Skip to content

Instantly share code, notes, and snippets.

@alexhiggins732
Created April 19, 2023 19:47
Show Gist options
  • Save alexhiggins732/1b6197c35345c07db8472a4fe3211e9d to your computer and use it in GitHub Desktop.
Save alexhiggins732/1b6197c35345c07db8472a4fe3211e9d to your computer and use it in GitHub Desktop.
Fody Model Change Tracker
using PropertyChanged;
using System.ComponentModel;
namespace INPCProxy
{
// install package PropertyChanged.Fody
internal class FodyChangeTrackingExample
{
public static void ModifyPerson()
{
var person = new FodyPerson { FirstName = "John", LastName = "Doe" };
person.PropertyChanged += Person_PropertyChanged;
person.FirstName = "Jane"; // The PropertyChanged event will be automatically called
person.FirstName = "Janet";
}
private static void Person_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is null || e is null || e.PropertyName is null) return;
var t = sender.GetType();
var value = t.GetProperty(e.PropertyName)?.GetValue(sender);
Console.WriteLine($"{t.Name}.{e.PropertyName} changed to {value}");
}
}
// Make model partial and add [AddINotifyPropertyChangedInterface]
// Fody will inject inotifypropertychange interface, event and setter into code.
// see: https://github.com/Fody/PropertyChanged
[AddINotifyPropertyChangedInterface]
public partial class FodyPerson
{
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment