我有一个与此类似的属性:
private ISomething currentSomething;
public ISomething CurrentSomething
{
get { return currentSomething; }
set
{
if (!object.Equals(currentSomething, value))
{
currentSomething = value;
RaisePropertyChanged(() => CurrentSomething);
}
}
}
调用setter时,该值会正确转移到currentSomething
字段。但是,在RaisePropertyChanged(() => CurrentSomething);
行之后,currentSomething字段中的一个列表删除了两个项目。
我似乎很清楚,我的代码中的某些内容订阅了此属性更改的事件(并且修剪了我需要单独留下的列表)。但是,我似乎无法找到这个处理程序。
有没有办法找到RaisePropertyChanged事件的所有订阅者?
更新
我弄明白我的对象是什么。它被绑定到一个控件,它有一个视图模型,正在做它的东西。我会打开这个问题以防有人得到一个好的答案,但我不再被卡住了。
答案 0 :(得分:4)
您可以对事件使用GetInvocationList()方法。如果你在myObject(类型为MyClass)实例上有一个PropertyChanged事件,那么你可以得到这样的订阅者:
var methodInfo = typeof (MyClass.PropertyChangedDelegate).GetMethod("GetInvocationList");
var p = myObject.GetType().GetField("PropertyChanged", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(myObject);
var subscriberDelegates = (Delegate[])methodInfo.Invoke(p, null);
object[] subscriberObjects = subscriberDelegates.Select(sub => sub.Target).ToArray();
这甚至可以在定义事件的类之外工作。