C# 如何查找谁订阅了事件字段?
我想知道在基类中何时设置PropertyChanged事件处理程序 Debug.Print(“为Null”)和Debug.Print(“非Null”)都会被命中。所以它一定是被设定在某个地方。 我怎么知道呢 搜索PropertyChanged不会显示订阅ebvent的代码C# 如何查找谁订阅了事件字段?,c#,winforms,event-handling,inotifypropertychanged,propertychanged,C#,Winforms,Event Handling,Inotifypropertychanged,Propertychanged,我想知道在基类中何时设置PropertyChanged事件处理程序 Debug.Print(“为Null”)和Debug.Print(“非Null”)都会被命中。所以它一定是被设定在某个地方。 我怎么知道呢 搜索PropertyChanged不会显示订阅ebvent的代码 public abstract class NonPersistentObjectBase : INotifyPropertyChanged, IObjectSpaceLink { public event P
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);
}
}
我们不能在没有赋值的情况下为这样一条语句添加断点 我们只能添加运行时指令 void声明以及方法签名本身并不是要执行的真正指令:没有赋值、没有调用、没有循环、没有测试、没有跳转、没有计算。。。只是编译器在编译时计划和完成的“静态内存保留” 但是我们可以在一个真正的私有字段上实现属性事件,因此我们可以设置断点 此外,完成后,我们可以在VisualStudio中打开调用堆栈窗口,或者退出方法,指向调用方订户
public event PropertyChangedEventHandler PropertyChanged
{
add => _PropertyChanged += value;
remove => _PropertyChanged -= value;
}
private event PropertyChangedEventHandler _PropertyChanged
这就是答案。要不要写下来?谢谢。事实证明,该事件是在xaf NonPersistentObjectSpace.SubscribeToObjectEvents中订阅的
public event PropertyChangedEventHandler PropertyChanged
{
add => _PropertyChanged += value;
remove => _PropertyChanged -= value;
}
private event PropertyChangedEventHandler _PropertyChanged