gpt4 book ai didi

c# - PropertyDescriptor.AddValueChanged 如何工作?

转载 作者:太空宇宙 更新时间:2023-11-03 21:52:31 26 4
gpt4 key购买 nike

我对这个方法的细节很感兴趣。如何以及通过什么来跟踪属性(property)的值(value)变化?

最佳答案

Andrew Smith 有一个 cool blog post关于它,我认为它会对您有所帮助。

The solution that I have seen given for this is to get to the PropertyDescriptor and use its AddValueChanged method to provide an EventHandler to receive a notification when the property has changed. Sometimes, the reply will mention/use DependencyPropertyDescriptor directly but its the same thing since that is just a derived PropertyDescriptor that provides additional information about the underlying DependencyProperty it represents. You can get to this property descriptor in a few ways but the most common are to get it from the TypeDescriptor.GetProperties method or using the DependencyPropertyDescriptor.FromProperty.

The issue with this approach is that it will root your object so it will never get collected by the GC. There have been plenty of discussions about how hooking events (particularly static events) can root your object so I won’t go into great detail there. While it does not seem that you are hooking a static event in this case, in essence you are. When you add a handler to a property descriptor, that property descriptor stores the delegate in a hashtable keyed by the object whose property you are hooking. A delegate/handler is basically a pointer to a method on an object (or no object if its for a static method) so that means the property descriptor has a reference to your object as well as the object whose value you are watching (since that is the key into the hashtable). The property descriptors themselves are cached statically so the hashtable is kept around and therefore your object and the one you are watching are as well.

关于c# - PropertyDescriptor.AddValueChanged 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com