为什么 MemoryStream.GetBuffer 和 MemoryStream.Close 在 NetCoreApp 2 中不存在?

Why does MemoryStream.GetBuffer and MemoryStream.Close not exist in NetCoreApp 2?

我想将我的旧项目转换为 netcoreapp2.0 但我遇到了三个问题

为什么 NetCoreApp 2 中不存在以下 API?

  1. MemoryStream.GetBuffer()
  2. MemoryStream.Close()
  3. BinaryWriter.Close()

虽然这些功能似乎已经实现了

netstandard2.0_diff

.NET Core API Reference version 2.0

我如何访问这些?

GetBuffer() 已删除。您应该使用 TryGetBuffer() 而不是 GetBuffer() 方法。如果你有兴趣,为什么 (source):

the reason GetBuffer is removed is because it is broken in the general case and would only work for folks that actually constructed the MemoryBuffer themselves and know the exact part of the underlying buffer that is being used. For example, there are constructors on MemoryBuffer that take the buffer and a start index and that start index is not at all reflected in what is returned from GetBuffer so other people that call that without knowing the start index will potentially be looking at memory outside the MemoryStream. That is why TryGetBuffer uses an ArraySegment so it can communicate the start index and the length to the callers so they can properly scope to the underlying buffer being used by MemoryStream.


Close() 方法作为 Dispose 的重复被删除。来自原文 issue:

using (var ms = new MemoryStream())
{
// ...
} // automatically Disposed(Closed) here

或直接调用

var ms = new MemoryStream();
// ...
ms.Dispose() // Closed here

更新:看起来有关 Close 的信息仅对 .NET Core < 2.0 有效。

Here 是 .NET Core 2.0 的 Close() 方法描述。有趣的是无论如何都不推荐使用它:

Instead of calling this method, ensure that the stream is properly disposed. This method calls Dispose, specifying true to release all resources. You do not have to specifically call the Close method. Instead, ensure that every Stream object is properly disposed.


关闭方法在.NET Core 2.0中可用,我刚刚安装了VS 2017 Prev-5.13,其中包含.Net Core 2.0 Preview,我可以使用关闭方法。

.NET Core / .NET Standard 中的新策略是尽可能保持 API 兼容性(虽然不是完全兼容性),以便大多数构建的二进制文件与新生态系统兼容.

一些之前清理 API 的决定被撤销,以在不特定 Windows 的情况下保持兼容性。

我发现了问题,我使用 VS 2017 15.2 创建项目然后使用 dotnet core 2 构建它所以 我必须删除 obj 和 bin 文件夹 然后重新构建,似乎在 obj 文件夹中设置了一些依赖项设置。