Replies: 4 comments
-
Unfortunately I don't think that the breaking change is avoidable. C# doesn't have explicit overrides, so you can't have both your own |
Beta Was this translation helpful? Give feedback.
-
A solution for this scenario is proposed in #1618. |
Beta Was this translation helpful? Give feedback.
-
If it's acceptable to insert an intermediate base class, then yes. // This cannot be changed because it comes from .NET.
public abstract class Stream : IAsyncDisposable
{
public virtual ValueTask DisposeAsync() { /* omitted*/ }
}
// Base class cannot be less accessible than derived class.
public abstract class Helper : Stream, IAsyncDisposable
{
// This prevents outsiders from using this class.
internal Helper() { }
// It's the simplest to just call the last overriding of IAsyncDisposable.DisposeAsync, but
// you can also create a whole new (protected) abstract method and call it.
public sealed override ValueTask DisposeAsync() { return ((IAsyncDisposable)this).DisposeAsync(); }
// If you forget to re-implement IAsyncDisposable,
// you get NotImplementedException instead of a mysterious StackOverflowException.
ValueTask IAsyncDisposable.DisposeAsync() { throw new NotImplementedException(); }
}
public sealed class MyStream : Helper, IAsyncDisposable
{
ValueTask IAsyncDisposable.DisposeAsync() { /* omitted */ }
public new Task DisposeAsync() { /* omitted */ }
} Edited: |
Beta Was this translation helpful? Give feedback.
-
That's a clever solution. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Before .NET Core introduced the Stream.DisposeAsync() method that returns
ValueTask
, I had already defined a Task-returningDisposeAsync
method on my own Stream-derived type.Now when I target .NET Core 3.1 from my library, I get warning CS0114 about hiding instead of inheriting from
Stream.DisposeAsync()
.I already define an explicit interface implementation:
What I want is my class to override the
Stream.DisposeAsync
method of course, so that polymorphism can do its work when folks callStream.DisposeAsync()
on my object. But I cannot override the base method without removing my own method. Since the return type is different, this is an API binary breaking change, and potentially a source breaking change as well.The C# language does not give me the ability to define two public methods with the same name but different return types, where one overrides a base member and the other one defines its own.
Is there no way to target .NET Core 3.x in my library, override the
DisposeAsync
method without an API breaking change?Beta Was this translation helpful? Give feedback.
All reactions