Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a base-class explicit interface method in F#

Ok I derive a type B from a base class A. A implements IDisposable explicit but I have to do additional cleanup in B, so I implement IDisposable in B:

interface IDisposable with
    member i.Dispose() =
        // ... additional work
        base.Dispose() // <- want to do but cannot

Question is: how to access the Dispose-method from base?

(base :> IDisposable).Dispose()

yields compiler error: Unexpected symbol ':>' in expression. Expected '.' or other token.

Doing something like

(i :> IDisposable).Dispose()

of course yields a StackOverflowException on runtime - so how can I do this? Sorry but never encountered something like this before...

like image 520
Random Dev Avatar asked Mar 05 '12 14:03

Random Dev


People also ask

How do you call an explicit interface?

The following is an example of how to call "Explicit Interface Method" in the same class using class methods. Output: Class1 Display Method. Iinterface_1 Method Explicit interface implementation. Iinterface_1 Method Implicit interface implementation.

Can a base class implement an interface?

A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members. For more information about virtual members, see Polymorphism.

How do you call an interface method in C#?

In order to call the methods using interface reference(here r is interface reference), you have to assign to class object to it. Like if you are assigning Person1's object obj1 to r i.e. r = obj1; then you call the Speed() and Distance() methods that are implemented by the Person1 class.

How do you call interface method in controller?

You could create a folder called Interface, then you need to create the interface in the folder. Next, you could create the class that implement this interface in the Model folder. Finally, you could use the class that implement this interface in controller.


1 Answers

You're probably better off putting your clean-up logic in a virtual method and implementing IDisposable only once.

type A() =
  abstract Close : unit -> unit
  default __.Close() =
    printfn "Cleaning up A"
  interface System.IDisposable with
    member this.Dispose() = this.Close()

type B() =
  inherit A()
  override __.Close() = 
    printfn "Cleaning up B"
    base.Close()

Since there's no protected access modifier, you can use a signature file to make Close non-public (or mark it internal).

The base keyword can only be used for member access, not standalone. That's why base :> IDisposable doesn't work.

Looking in Reflector, Dispose only calls the public Close method. So you could re-implement IDisposable and call base.Close() instead.

You could have this same scenario in C#. Inheritable classes that implement IDisposable should provide a way for subclasses to "plug in" to disposal. This is usually done by providing a protected virtual Dispose(disposing) overload that's called from Dispose(). For whatever reason, DuplexClientBase doesn't follow this convention. Perhaps it was deemed unnecessary given that Dispose merely forwards to Close.

like image 189
Daniel Avatar answered Sep 22 '22 09:09

Daniel