如何找出发布到 Dispatcher 的确切操作
有没有一种方法可以让我们找出哪个 UI 元素已将操作发布到 Dispatcher 队列并最终引发事件,System.Windows.Threading.Dispatcher.CurrentDispatcher.Hooks.OperationPosted
Update :私有属性 DispatcherOperation.Name
显示了我在调试模式下 VS 鼠标悬停时需要的内容。我只需将此名称打印到记录器以用于其他调试目的。是否可以动态提取Name
。
Is there a way where we can find out which UI element has posted an operation to the Dispatcher queue which eventually throws the event ,System.Windows.Threading.Dispatcher.CurrentDispatcher.Hooks.OperationPosted
Update : A private property DispatcherOperation.Name
is showing what I need in the VS mouse-over in debugging mode. I just need to print this name to a logger for other debugging purposes. Is it possible to extract the Name
dynamically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的,虽然我给你一种方法来做到这一点,但我必须警告你,使用反射来获取私有或受保护的字段/属性/方法从来都不是一个好主意,因为首先它们通常是私有的,这是有原因的,其次是如果签名或接口发生变化,您的代码可能会崩溃。但因为你说它只是为了调试,所以这可能是一个有价值的解决方案。
您始终可以对这些使用 Reflection之类的事情。首先,您需要要读取其私有属性的对象的类型和实例。不幸的是,我不知道您正在寻找的名称是字段还是属性,但总体过程是相似的。首先使用 GetProperty 获取属性,然后调用 GetValue 返回的
PropertyInfo
目的。您可能希望缓存 PropertyInfo 对象以在调试时获得一些速度。您还需要再次使用正确的 BindingFlags 我不知道不确切知道如何描述字段/属性,因此我无法为您提供确切的代码,但从这里应该很容易弄清楚。希望有帮助。
Yes it is possible, while i give you a way to do it, i must warn you though, using reflection to get private or protected fields/properties/methods is never a good idea because first they are usually private for a reason and second of all if the signature or interface changes you code might break. But because you said it is just for debugging, this might be a valuable solution.
You can always use Reflection for these kind of things. First you need the Type and Instance of the object you want to read its private properties. Unfortunately i don't know if the Name you are looking for is a field or a property, but the overall procedure is similar. First get the property with GetProperty and then call GetValue on the returned
PropertyInfo
object. You might want to cache the PropertyInfo object to gain some speed while debug. You also need to use the correct BindingFlags again i don't know exactly how the field/property is described so i can't give you the exact code, but from here it should be easy to figure out.Hope that helps.