Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

f# - How to access protected member

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)

The member or object constructor 'ConstructPageReference' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.

Update:

I have tried following but getting the same result

let getPagereference id =
    base.ConstructPageReference(id)

Update 2 (solution):

here is the code as it was:

type MyNewType() =
    inherit SomeAbstractType()

    let getPagereference id =
        base.ConstructPageReference(id)

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

this is how it should have been:

type MyNewType() =
    inherit SomeAbstractType()

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

    override this.SomeMethod()=
       let id = 0
       let pr = this.ConstructPageReference(id)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Gabe is correct. Your code:

let getPagereference id =
  this.ConstructPageReference(id)

is the same as

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...