Overriding Events in VB

Is there a way to translate this code in VB? Most of them are simple, but I cannot find a way to override the event handler.

public class MTObservableCollection<T> : ObservableCollection<T> { 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); } } } } 
+4
source share
2 answers

It is even a mistake to redefine events in C #. C # Programming Guide :

Do not declare virtual events in the base class and override them in the derived class. The C # compiler does not handle them correctly in Microsoft Visual Studio 2008, and it is unpredictable whether the subscriber's derived event will actually be subscribing to the base class event.

I wonder why the framework class violates this rule or even why the compiler allows it.

+1
source

Rewrite Edit:

A conversation that shows that the snafu compiler implementation and offers workarounds:

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

This is my (semi) final answer.

+2
source

Source: https://habr.com/ru/post/1301377/


All Articles