如何访问受保护的成员 [英] How to access protected member

查看:109
本文介绍了如何访问受保护的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下扩展类型的代码(在F#中),它调用从其继承的类的受保护方法(在C#中) 但我得到了例外(见下文).有解决方法吗?

I have following code in extending type (in F#) which invokes a protected method of a class it inherits from (in C#) but I get the exception (see below). Is there a workaround for this?

let getPagereference id =
    this.ConstructPageReference(id)

成员或对象构造函数'ConstructPageReference'无法访问.私有成员只能在声明类型内访问.受保护的成员只能从扩展类型访问,而不能从内部Lambda表达式访问.

更新:

我曾尝试关注但获得相同的结果

I have tried following but getting the same result

let getPagereference id =
    base.ConstructPageReference(id)

更新2(解决方案):

这是原来的代码:

type MyNewType() =
    inherit SomeAbstractType()

    let getPagereference id =
        base.ConstructPageReference(id)

    override this.SomeMethod()=
       let id = 0
       let pr = getPagereference id

这应该是这样的:

type MyNewType() =
    inherit SomeAbstractType()

    member this.ConstructPageReference(id) =
        base.ConstructPageReference(id)

    override this.SomeMethod()=
       let id = 0
       let pr = this.ConstructPageReference(id)

推荐答案

Gabe是正确的.您的代码:

Gabe is correct. Your code:

let getPagereference id =
  this.ConstructPageReference(id)

let getPagereference = fun id ->
  this.ConstructPageReference(id)

,因此您暗中尝试从lambda表达式内调用基本方法.您将需要从成员而不是让约束函数执行此操作.

and you are therefore implicitly attempting to call a base method from within a lambda expression. You will need to do this from a member, rather than a let-bound function.

这篇关于如何访问受保护的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆