I want to find out when the PropertyChanged event handler is set in my base class.
Both Debug.Print("Is Null") and Debug.Print("not null") get hit. So it must get set somewhere. How do I find that out?
A search for PropertyChanged does not reveal code that subscribes to the ebvent.
public abstract class NonPersistentObjectBase : INotifyPropertyChanged, IObjectSpaceLink {
public event PropertyChangedEventHandler PropertyChanged; // how do I break here
protected void OnPropertyChanged(string propertyName) {
if(PropertyChanged != null) {
Debug.Print("not null"); // gets hit after I click save
}
else {
Debug.Print("Is Null"); //gets hit
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected void SetPropertyValue<T>(string name, ref T field, T value) {
if(!Equals(field, value)) {
field = value;
OnPropertyChanged(name);
}
}
I added a private event as per Olivier's suggestion but am unsure how to call it. I tried assigning it in the constructor
private event PropertyChangedEventHandler PropertyChangedAdd {
add => PropertyChanged += value;
remove => PropertyChanged -= value;
}
We cannot add a breakpoint to such a single statement without an assignment.
We can only add on a runtime instruction.
A void declaration as well as a method signature alone is not an real instruction to be executed: no assignment, no call, no loop, no test, no jump, no calculation... just a "static memory reservation" planned and done by the compiler at compile-time.
But we can implement add and remove accessors of a property-event on a real private field, thus we will be able to put breakpoints.
Also, once that done, we can open the call stack window in Visual Studio or go out of the method toward the caller subscriber.
public event PropertyChangedEventHandler PropertyChanged
{
add => _PropertyChanged += value;
remove => _PropertyChanged -= value;
}
private event PropertyChangedEventHandler _PropertyChanged