Csharp/C#教程:在VB中覆盖事件分享


在VB中覆盖事件

有没有办法在VB中翻译这段代码? 大部分都很简单,但我无法想出一种覆盖事件处理程序的方法。

public class MTObservableCollection : ObservableCollection { public MTObservableCollection() { _DispatcherPriority = DispatcherPriority.DataBind; } public MTObservableCollection(DispatcherPriority dispatcherPriority) { _DispatcherPriority = dispatcherPriority; } private DispatcherPriority _DispatcherPriority; public override event NotifyCollectionChangedEventHandler CollectionChanged; protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { var eh = CollectionChanged; if (eh != null) { Dispatcher dispatcher = (from NotifyCollectionChangedEventHandler nh in eh.GetInvocationList() let dpo = nh.Target as DispatcherObject where dpo != null select dpo.Dispatcher).FirstOrDefault(); if (dispatcher != null && dispatcher.CheckAccess() == false) { dispatcher.Invoke(DispatcherPriority.DataBind, (Action)(() => OnCollectionChanged(e))); } else { foreach (NotifyCollectionChangedEventHandler nh in eh.GetInvocationList()) nh.Invoke(this, e); } } } } 

在C#中覆盖事件甚至是错误的。 C#编程指南说:

不要在基类中声明虚拟事件,并在派生类中重写它们。 C#编译器在Microsoft Visual Studio 2008中无法正确处理这些问题,并且派生事件的订阅者是否实际上将订阅基类事件是不可预测的。

我想知道为什么框架类会破坏这个规则,甚至编译器为什么允许它。

重写编辑:

一个对话,它认为它是一个编译器实现snafu并建议解决方法:

https://social.msdn.microsoft.com/Forums/en/vblanguage/thread/ce30ceed-c260-4d99-b96d-5b7179466be8

这是我的(半)最终答案。

上述就是C#学习教程:在VB中覆盖事件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/954970.html

(0)
上一篇 2021年11月20日
下一篇 2021年11月20日

精彩推荐