假设我们有以下C#类
public class Class1
{
protected event EventHandler ProtectedEvent;
protected virtual void OverrideMe() { }
}
Run Code Online (Sandbox Code Playgroud)
似乎不可能在F#中使用ProtectedEvent.
type HelpMe() as this =
inherit Class1()
do
printfn "%A" this.ProtectedEvent
member x.HookEvents() =
printfn "%A" x.ProtectedEvent
member private x.HookEvents2() =
printfn "%A" x.ProtectedEvent
override x.OverrideMe() =
printfn "%A" x.ProtectedEvent
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我试图调用printfn它,因为有多种方法可以在F#中连接事件,我希望很清楚,这只是导致问题的事件的引用.
在上面的每种情况下,编译器都会抱怨以下错误
被调用的受保护成员或正在使用"基础".这只能在成员的直接实现中使用,因为它们可以逃避它们的对象范围.
我理解这个错误,是什么导致它及其目的.通常,解决方法是将调用包装在私有成员中,该方法适用于方法 - 但这似乎不适用于事件.无论我尝试什么,似乎都不可能在F#中使用受保护的事件,除非我求助于使用反射做某事,或者对基类做一些更改(在我的情况下是不可能的).
请注意,我也尝试了所有可能的使用组合base,this和x.
难道我做错了什么 ?
f# ×1