有没有办法在VB中翻译这段代码?大部分都很简单,但我想不出覆盖事件处理程序的方法。
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);
}
}
}
}
最佳答案
在 C# 中覆盖事件甚至是一个错误。 C# Programming Guide 说:
我想知道为什么一个框架类打破了这个规则,甚至为什么编译器允许它。