已重命名或删除的 OSPlatform 属性

.NET 5 预览版 8 中引入的以下属性已被删除或重命名: MinimumOSPlatformAttributeRemovedInOSPlatformAttribute以及 ObsoletedInOSPlatformAttribute

更改描述

.NET 5 预览版 8 在命名空间中 System.Runtime.Versioning 引入了以下属性:

  • MinimumOSPlatformAttribute
  • RemovedInOSPlatformAttribute
  • ObsoletedInOSPlatformAttribute

在 .NET 5 预览版 8 中,当项目使用 目标框架标识符(例如 net5.0-windows)来面向特定操作系统的 .NET 5 版本时,生成过程将添加程序集级别的 System.Runtime.Versioning.MinimumOSPlatformAttribute 属性。

在 .NET 5 RC1 中,ObsoletedInOSPlatformAttribute 已被删除,MinimumOSPlatformAttributeRemovedInOSPlatformAttribute 已按如下方式重命名:

预览版 8 名称 RC1 及更高版本名称
MinimumOSPlatformAttribute SupportedOSPlatformAttribute
RemovedInOSPlatformAttribute UnsupportedOSPlatformAttribute

在 .NET 5 RC1 及更高版本中,当项目使用 目标框架名称(例如 net5.0-windows)来定位特定操作系统的 .NET 5 风格时,构建过程中会添加程序集级别的 SupportedOSPlatformAttribute 属性。

更改原因

.NET 5 预览版 8 引入了属性 System.Runtime.Versioning ,用于指定 API 支持的平台。 当在不支持特定 API 的平台上使用这些 API 时,平台兼容性分析器 会使用这些属性来生成构建警告。

对于 .NET 5 RC1,向平台兼容性分析器添加了一项附加功能,用于平台排除。 此功能允许在 OS 平台上将 API 标记为完全不受支持。 此功能提示更改属性,包括使用更合适的名称。 已删除 ObsoletedInOSPlatformAttribute ,因为它不再需要。

已引入的版本

5.0 RC1

将项目从 .NET 5 预览版 8 重新定位到 .NET 5 RC1 时,可能会遇到这些更改导致的生成或运行时错误。 例如,重命名 MinimumOSPlatformAttribute 可能会生成错误,因为属性在生成时应用于特定于平台的程序集,旧的生成项目仍将引用旧的 API 名称。

生成时错误示例:

  • 错误 CS0246:找不到类型名称或命名空间名称“MinimumOSPlatformAttribute”(是否缺少 using 指令或程序集引用?)
  • 错误 CS0246:找不到类型名称或命名空间名称“RemovedInOSPlatformAttribute”(是否缺少 using 指令或程序集引用?)
  • 错误 CS0246:找不到类型名称或命名空间名称“ObsoletedInOSPlatformAttribute”(是否缺少 using 指令或程序集引用?)

示例运行时错误:

未经处理的异常。 System.TypeLoadException:无法从程序集“System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Runtime.Versioning.MinimumOSPlatformAttribute”。

若要解决以下错误,

  • MinimumOSPlatformAttribute 的任何引用更新为 SupportedOSPlatformAttribute
  • RemovedInOSPlatformAttribute 的任何引用更新为 UnsupportedOSPlatformAttribute
  • 删除对 ObsoletedInOSPlatformAttribute. 的任何引用。
  • 重新构建项目(或执行清理 + 构建)以删除旧的构建产物。

受影响的 API

  • System.Runtime.Versioning.MinimumOSPlatformAttribute
  • System.Runtime.Versioning.ObsoletedInOSPlatformAttribute
  • System.Runtime.Versioning.RemovedInOSPlatformAttribute