Why does MemoryStream.GetBuffer() always throw?

后端 未结 4 1581
滥情空心
滥情空心 2021-01-03 22:21

The following code will always throw UnuthorizedAccessException (MemoryStream\'s internal buffer cannot be accessed.)

byte[] buf1 = { 2, 3, 5, 7, 11 };
var         


        
4条回答
  •  执念已碎
    2021-01-03 22:40

    Here is the documentation for MemoryStream(byte[]) constructor that you're using. It specifically says:

    This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.

    You should use this constructor instead, with publiclyVisible = true.

提交回复
热议问题