Created
April 19, 2023 19:47
-
-
Save alexhiggins732/1b6197c35345c07db8472a4fe3211e9d to your computer and use it in GitHub Desktop.
Fody Model Change Tracker
This file contains hidden or 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
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