E
Eugene
I'm trying to write a class which uses BinaryWriter as its base but allows
for queuing of write requests
Public Class QueuedBinaryWriter
Inherits BinaryWriter
I override all the Write methods like so:
Public Overloads Overrides Sub Write(ByVal Value As Byte)
m_Queue.Enqueue(New WriteRequest(MyBase.BaseStream.Position, Value))
End Sub
'same for all other Write variants
I also add an overloaded Write method for my own datatype
private Overloads Sub Write(ByVal Request As WriteRequest)
Dim t As Type = Request.Type
If t Is GetType(Boolean) Then
MyBase.Write(DirectCast(Request.Value, Boolean))
ElseIf t Is GetType(Byte) Then
MyBase.Write(DirectCast(Request.Value, Byte))
' etc...
end sub
and finally I override the Flush() method
Public Overrides Sub Flush()
For Each Request As WriteRequest In m_Queue
Seek(CInt(Request.Offset), SeekOrigin.Begin)
Write(Request)
Next
MyBase.Flush()
m_Queue.Clear()
End Sub
The problem is in the MyBase.Write() call in the Write(WriteRequest) method.
It doesn't call the base class Write but instead calls
QueuedBinaryWriter.Write(). This leads to an infinite loop since each
QueuedBinaryWriter.Write() enqueues another WriteRequest.
I checked the IL:
IL_0009: ldtoken [mscorlib]System.Boolean
IL_000e: call class [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle(valuetype
[mscorlib]System.RuntimeTypeHandle)
IL_0013: bne.un.s IL_0031
IL_0015: ldarg.0
IL_0016: ldarg.1
IL_0017: ldfld object
QueuedBinaryWriter.ciloci.QueuedBinaryWriter/WriteRequest::Value
IL_001c: unbox [mscorlib]System.Boolean
IL_0021: ldobj [mscorlib]System.Boolean
IL_0026: call instance void
[mscorlib]System.IO.BinaryWriter::Write(bool)
Any suggestions on how to solve this problem?
Thank you for your time.
for queuing of write requests
Public Class QueuedBinaryWriter
Inherits BinaryWriter
I override all the Write methods like so:
Public Overloads Overrides Sub Write(ByVal Value As Byte)
m_Queue.Enqueue(New WriteRequest(MyBase.BaseStream.Position, Value))
End Sub
'same for all other Write variants
I also add an overloaded Write method for my own datatype
private Overloads Sub Write(ByVal Request As WriteRequest)
Dim t As Type = Request.Type
If t Is GetType(Boolean) Then
MyBase.Write(DirectCast(Request.Value, Boolean))
ElseIf t Is GetType(Byte) Then
MyBase.Write(DirectCast(Request.Value, Byte))
' etc...
end sub
and finally I override the Flush() method
Public Overrides Sub Flush()
For Each Request As WriteRequest In m_Queue
Seek(CInt(Request.Offset), SeekOrigin.Begin)
Write(Request)
Next
MyBase.Flush()
m_Queue.Clear()
End Sub
The problem is in the MyBase.Write() call in the Write(WriteRequest) method.
It doesn't call the base class Write but instead calls
QueuedBinaryWriter.Write(). This leads to an infinite loop since each
QueuedBinaryWriter.Write() enqueues another WriteRequest.
I checked the IL:
IL_0009: ldtoken [mscorlib]System.Boolean
IL_000e: call class [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle(valuetype
[mscorlib]System.RuntimeTypeHandle)
IL_0013: bne.un.s IL_0031
IL_0015: ldarg.0
IL_0016: ldarg.1
IL_0017: ldfld object
QueuedBinaryWriter.ciloci.QueuedBinaryWriter/WriteRequest::Value
IL_001c: unbox [mscorlib]System.Boolean
IL_0021: ldobj [mscorlib]System.Boolean
IL_0026: call instance void
[mscorlib]System.IO.BinaryWriter::Write(bool)
Any suggestions on how to solve this problem?
Thank you for your time.