7

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)
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Enes
  • 3,951
  • 3
  • 25
  • 23
  • F# (and AFAIK all CLI languages) honors access modifiers: http://msdn.microsoft.com/en-us/library/ms173121.aspx – Mauricio Scheffer Mar 02 '10 at 20:31
  • Or maybe I didn't understand the question... – Mauricio Scheffer Mar 02 '10 at 20:32
  • well say that to f# interactive – Enes Mar 02 '10 at 20:33
  • But if you say that it's a protected method, it's ok that you can't access it from outside. Please post more code. Where is `getPagereference` defined? – Mauricio Scheffer Mar 02 '10 at 20:36
  • OK you're right I wasn't clear. The code is in the F# type that inherits from the C# abstract class – Enes Mar 02 '10 at 20:39
  • 3
    Again: please post more code. Where is `getPagereference` defined? Where does it get the `this` reference from? `let` bindings are not intended to define instance methods (use `member` instead) – Mauricio Scheffer Mar 02 '10 at 21:02
  • In this case, you shouldn't need to declare your own `this.ConstructPageReference` method; you can still call the base method directly from within other members. – kvb Mar 02 '10 at 23:00

2 Answers2

8

I bet the key part is the cannot be accessed from inner lambda expressions. You are probably trying to do the access from within a lambda.

Have you tried

member this.getPagereference(id) = 
    this.ConstructPageReference(id) 
Gabe
  • 84,912
  • 12
  • 139
  • 238
8

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.

kvb
  • 54,864
  • 2
  • 91
  • 133