流派生类型中的一些参数已重命名
在 .NET 6 中,派生自 System.IO.Stream 的类型上的方法的某些参数已重命名以匹配基类。
更改描述
在之前的 .NET 版本中,一些派生自 Stream 的类型会替代方法,但使用与基类使用的名称不同的参数名称。 例如,DeflateStream.Read(Byte[], Int32, Int32) 的字节数组参数命名为 array
,而基类方法中相应的自变量被命名为 buffer
。
在 .NET 6 中,所有派生自 System.IO.Stream 但其参数命名不匹配的类型都已通过使用与基类相同的参数名称,变得与基类保持一致。
引入的版本
.NET 6
更改原因
进行此更改的几项原因如下所示:
- 如果传递的自变量无效且引发了异常,则该异常可能包含基本参数的名称或所派生的参数的名称,具体取决于实现。 调用方可能已在使用类型为基类或派生类型的引用,因此异常中的自变量名称没法始终正确。
- 具有不同的参数名称使得更难在所有 Stream 实现中一致地验证行为。
- .NET 6 在 Stream 上添加了一个公共方法用于验证自变量,而需要具有一致的参数名称才能使用该方法。
建议的操作
此中断性变更带来的影响微乎其微:
- 对于现有的二进制文件,它仅影响使用反射来检查受影响的派生类型上的参数名称的代码。
- 对于源代码,它仅影响使用命名参数在派生的流类型上通过具有该派生类型的变量调用方法的代码。
在这两种情况下,都建议一致地使用基本参数名称。
受影响的 API
- System.IO.BufferedStream.Read(Byte[], Int32, Int32)
- System.IO.BufferedStream.Write(Byte[], Int32, Int32)
- System.IO.Compression.DeflateStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)
- System.IO.Compression.DeflateStream.Read(Byte[], Int32, Int32)
- System.IO.Compression.DeflateStream.ReadAsync(Byte[], Int32, Int32, CancellationToken)
- System.IO.Compression.DeflateStream.Write(Byte[], Int32, Int32)
- System.IO.Compression.DeflateStream.WriteAsync(Byte[], Int32, Int32, CancellationToken)
- System.IO.Compression.GZipStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)
- System.IO.Compression.GZipStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)
- System.IO.Compression.GZipStream.Read(Byte[], Int32, Int32)
- System.IO.Compression.GZipStream.ReadAsync(Byte[], Int32, Int32, CancellationToken)
- System.IO.Compression.GZipStream.Write(Byte[], Int32, Int32)
- System.IO.Compression.GZipStream.WriteAsync(Byte[], Int32, Int32, CancellationToken)
- System.IO.FileStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)
- System.IO.FileStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)
- System.IO.FileStream.Read(Byte[], Int32, Int32)
- System.IO.FileStream.Write(Byte[], Int32, Int32)
- System.Net.Sockets.NetworkStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)
- System.Net.Sockets.NetworkStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)
- System.Net.Sockets.NetworkStream.Read(Byte[], Int32, Int32)
- System.Net.Sockets.NetworkStream.ReadAsync(Byte[], Int32, Int32, CancellationToken)
- System.Net.Sockets.NetworkStream.Write(Byte[], Int32, Int32)
- System.Net.Sockets.NetworkStream.WriteAsync(Byte[], Int32, Int32, CancellationToken)
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈