无法调用受保护的基方法
Unable to call protected base method
我正在尝试在 F# 中实现以下 class(来自 https://peteohanlon.wordpress.com/2008/10/22/bulk-loading-in-observablecollection/):
public class RangeObservableCollection<T> : ObservableCollection<T>
{
private bool _suppressNotification = false;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (!_suppressNotification)
base.OnCollectionChanged(e);
}
public void AddRange(IEnumerable<T> list)
{
if (list == null)
throw new ArgumentNullException("list");
_suppressNotification = true;
foreach (T item in list)
{
Add(item);
}
_suppressNotification = false;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
这是简单的代码,端口也很简单:
type RangeObservableCollection<'T>() =
inherit ObservableCollection<'T>()
let mutable suppressNotification = false
override __.OnCollectionChanged(e: NotifyCollectionChangedEventArgs) =
if not suppressNotification
then base.OnCollectionChanged e
member __.AddRange(items: 'T seq) =
if isNull items
then ArgumentNullException "items" |> raise
suppressNotification <- true
items |> Seq.iter __.Add
suppressNotification <- false
NotifyCollectionChangedAction.Reset
|> NotifyCollectionChangedEventArgs
|> __.OnCollectionChanged
但是,我在最后一行遇到编译器错误,说
A protected member is called or 'base' is being used. This is only allowed in the direct implementation of members since they could escape their object scope.
我能找到的关于该错误的唯一参考是 this 问题;但是,这里的解决方案似乎并不适用,因为我不是在处理事件。
所以,我的问题分为两部分:
- language/compiler 实际问题是什么?
- 是否有解决此问题的方法,还是无法在 F# 中实现?
如果将最后三行更改为立即调用 OnCollectionChanged,则有效:
__.OnCollectionChanged(NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))
我认为问题在于 __.OnCollectionChanged
本身(即如果不立即调用)会产生对函数的引用,该函数可能会脱离 class 的上下文。 (在此示例中并非如此,但根据 F# 用于确定这一点的简单规则,编译器无法排除它)。
我正在尝试在 F# 中实现以下 class(来自 https://peteohanlon.wordpress.com/2008/10/22/bulk-loading-in-observablecollection/):
public class RangeObservableCollection<T> : ObservableCollection<T>
{
private bool _suppressNotification = false;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (!_suppressNotification)
base.OnCollectionChanged(e);
}
public void AddRange(IEnumerable<T> list)
{
if (list == null)
throw new ArgumentNullException("list");
_suppressNotification = true;
foreach (T item in list)
{
Add(item);
}
_suppressNotification = false;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
这是简单的代码,端口也很简单:
type RangeObservableCollection<'T>() =
inherit ObservableCollection<'T>()
let mutable suppressNotification = false
override __.OnCollectionChanged(e: NotifyCollectionChangedEventArgs) =
if not suppressNotification
then base.OnCollectionChanged e
member __.AddRange(items: 'T seq) =
if isNull items
then ArgumentNullException "items" |> raise
suppressNotification <- true
items |> Seq.iter __.Add
suppressNotification <- false
NotifyCollectionChangedAction.Reset
|> NotifyCollectionChangedEventArgs
|> __.OnCollectionChanged
但是,我在最后一行遇到编译器错误,说
A protected member is called or 'base' is being used. This is only allowed in the direct implementation of members since they could escape their object scope.
我能找到的关于该错误的唯一参考是 this 问题;但是,这里的解决方案似乎并不适用,因为我不是在处理事件。
所以,我的问题分为两部分:
- language/compiler 实际问题是什么?
- 是否有解决此问题的方法,还是无法在 F# 中实现?
如果将最后三行更改为立即调用 OnCollectionChanged,则有效:
__.OnCollectionChanged(NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))
我认为问题在于 __.OnCollectionChanged
本身(即如果不立即调用)会产生对函数的引用,该函数可能会脱离 class 的上下文。 (在此示例中并非如此,但根据 F# 用于确定这一点的简单规则,编译器无法排除它)。