How to capture old value and new value in INotifyPropertyChanged implementation of PropertyChanged event in C#

后端 未结 4 754
[愿得一人]
[愿得一人] 2021-01-19 16:03

I need to capture old value, new value and property name in PropertyChanged event handler. The default implementation of INotifyPropertyChanged provides only name of the pro

4条回答
  •  粉色の甜心
    2021-01-19 16:46

    I am not understanding how to implement generic interface on my Entity class because T will change depending on data type of property

    That's correct. The answer you're looking at isn't actually all that good an answer, because it contains code that doesn't compile or make sense.

    The basic idea of raising the non-generic PropertyChanged event by passing the generic args class that inherits the non-generic PropertyChangedEventArgs class is by itself okay. It should work fine in your case.

    But! You're not going to be able to implement the generic interface that answer proposes, because (as you already note) the interface doesn't work when the properties have different types. In fact, the answer you're looking at wouldn't even compile with that interface, because their event-raising method is itself generic, and raises the non-generic PropertyChanged event (the method would be incompatible with the generic interface), and they don't bother to actually implement their generic interface (if they'd tried, they'd have noticed that the generic method doesn't work with the generic interface…the method's type parameter T is different from the interface's type parameter T).

    It's unfortunate that highly up-voted answer was written that way. It has some useful ideas in it, but it also has completely unworkable ideas, and is going to just confuse exactly the people who are looking for an answer like that (i.e. the very people who most want to read and use the answer).

提交回复
热议问题