Why a BinaryWriter closes the outer Stream on disposal, and how to prevent that? (.NET C#)

后端 未结 4 1800
天命终不由人
天命终不由人 2020-12-06 09:06

I have one method that receives a Stream to write on it using a BinaryWriter. But when I dispose this BinaryWriter it also closes the stream. Can I leave it undisposed so I

4条回答
  •  生来不讨喜
    2020-12-06 09:48

    The protected BinaryWriter.Dispose(bool) method is virtual, and all it does is closes the stream (you can check that it is true in Reflector). -This method is called by the Dispose() method.

    You can simply inherit a class from the BinaryWriter and override the Dispose(bool) method to do nothing, or something else alltogether.

提交回复
热议问题