public override void Write(byte[] buffer, int offset, int size)
        {
            var message = $"{Name}.Write({offset},{size})";

            if (RequireAsync)
            {
                throw Context.AssertFail($"{message}: async API required.");
            }

            SyncWriteFunc syncWrite         = (b, o, s) => base.Write(b, o, s);
            SyncWriteFunc originalSyncWrite = syncWrite;

            var action = Interlocked.Exchange(ref writeAction, null);

            if (action?.AsyncWrite != null)
            {
                message += " - action";

                AsyncWriteFunc asyncBaseWrite = (b, o, s, _) => Task.Factory.FromAsync(
                    (callback, state) => originalSyncWrite.BeginInvoke(b, o, s, callback, state),
                    (result) => originalSyncWrite.EndInvoke(result), null);

                syncWrite = (b, o, s) => action.AsyncWrite(b, o, s, asyncBaseWrite, CancellationToken.None).Wait();
            }

            Write_internal(buffer, offset, size, message, syncWrite);
        }
        public override void Write(byte[] buffer, int offset, int size)
        {
            var message = string.Format("{0}.Write({1},{2})", Name, offset, size);

            SyncWriteFunc syncWrite         = (b, o, s) => base.Write(b, o, s);
            SyncWriteFunc originalSyncWrite = syncWrite;

            var action = Interlocked.Exchange(ref writeAction, null);

            if (action?.AsyncWrite != null)
            {
                message += " - action";

                AsyncWriteFunc asyncBaseWrite = (b, o, s, _) => Task.Factory.FromAsync(
                    (callback, state) => originalSyncWrite.BeginInvoke(b, o, s, callback, state),
                    (result) => originalSyncWrite.EndInvoke(result), null);

                syncWrite = (b, o, s) => action.AsyncWrite(b, o, s, asyncBaseWrite, CancellationToken.None).Wait();
            }

            Write_internal(buffer, offset, size, message, syncWrite);
        }