C# Extension failed to override base class method


(James Esword) #1

When I try to use C# Extension to refactor my cross platform IDispose interface, there’s a problem for iOS:

public interface IDispose
    #if COOPER
    : java.io.Closeable
    #elif ECHOES
    : System.IDisposable
    #endif
{
    void dispose();
}

public __extension class IDisposeDefault : IDispose {
    #if ECHOES
    public void Dispose() => dispose();
    #elif COOPER
    public void close() => dispose();
    #elif TOFFEE

    // This override won't take effect
    public override void dealloc() => dispose();

    #endif
}

public class TestDisposeClass : IDispose{
    public static bool isDisposed = false;
    public void dispose(){
        isDisposed = true;
    }
}

public class MyTest{
    public static void test(){
        using(var t = new TestDisposeClass()){
        }

        // result is true for ECHOES/COOPER, but false for TOFFEE
        var result = TestDisposeClass.isDisposed;
    }
}

(RemObjects) #2

Thanks, logged as bugs://75987


(Carlo Kok) #3

ah I see now what you mean. But that’s because extension don’t ever override, they just implement if needed. This is going to be a lot trickier than it sounds.


(RemObjects) #4

bugs://75987 got closed with status fixed.


(Carlo Kok) #5

Oke so in the next build you can do:

  public interface IDispose
    #if COOPER
    : java.io.Closeable
    #elif ECHOES
    : System.IDisposable
    #endif
{
    void dispose();

    #if TOFFEE
    void dealloc();
    #endif
}

(Note the #if TOFFEE block)

this requires the upcoming build to work though


(James Esword) #6

Thank you Carlo :+1: