How do I view the content of a Stream in the QuickWatch window within Visual Studio?
Update
As per Daniel's answer I used the following code -
System.Text.Encoding.UTF8.GetString((byte[])stream.GetType().GetMethod("InternalGetBuffer", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(stream, null))
It's available from Debug | Windows | Watch | Watch 1 or Ctrl + Alt + W + 1. There are 4 watch windows in Visual Studio, which you can use in different contexts (Watch 1, Watch 2, etc.). Any expression can be entered into the watch window.
On the Debug menu, choose Windows > Immediate.
View return values for functions If the window is closed, use Debug > Windows > Autos to open the Autos window. In addition, you can enter functions in the Immediate window to view return values. (Open it using Debug > Windows > Immediate.)
You can view the content of the MemoryStream
without altering it when you can make some assumptions:
MemoryStream
If you can make these assumptions, you can use the following code in your Watch window:
System.Text.Encoding.UTF8.GetString((byte[])stream.GetType().GetMethod("InternalGetBuffer", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(stream, null))
Disclaimer:
This might have side effects I haven't thought of or might throw an exception in certain circumstances, so don't use this in production code.
I don't believe there's anything generic built in, since QuickWatch is not generally designed to affect the state of what's being watched, and reading from a stream inherently alters the internal state (e.g. the current position) - even assuming that the stream can be read.
And even then, not all streams support seeking, so reading from the stream would then make the read data unavailable for the actual program, with no means to recover that data.
In limited circumstances, if you construct the MemoryStream from a byte
buffer, or if GetBuffer()
is applicable, a watch on the byte
buffer would be doable, rather than trying to watch the stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With