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)
Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.
Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class. For related information, see friend, public, private, and the member-access table in Controlling Access to Class Members.
We can access protected members of a class in its subclass present in a different package. In the following example, we will create two classes. Sample class in package1 and Child class in package2. Child class extends Sample class.
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With