已重新命名或移除 OSPlatform 屬性

.NET 5 Preview 8 中引入的下列屬性已移除或重新命名:MinimumOSPlatformAttributeRemovedInOSPlatformAttributeObsoletedInOSPlatformAttribute

變更描述

.NET 5 Preview 8 在 System.Runtime.Versioning 命名空間中引入了下列屬性:

  • MinimumOSPlatformAttribute
  • RemovedInOSPlatformAttribute
  • ObsoletedInOSPlatformAttribute

在 .NET 5 Preview 8 中,當專案使用目標 Framework Moniker (例如 net5.0-windows) 來鎖定 .NET 5 的 OS 特定變體時,組建便會新增組件等級的 System.Runtime.Versioning.MinimumOSPlatformAttribute 屬性。

在 .NET 5 RC1 中,ObsoletedInOSPlatformAttribute 已移除,而 MinimumOSPlatformAttributeRemovedInOSPlatformAttribute 已重新命名,如下所示:

Preview 8 名稱 RC1 和更新版本名稱
MinimumOSPlatformAttribute SupportedOSPlatformAttribute
RemovedInOSPlatformAttribute UnsupportedOSPlatformAttribute

在 .NET 5 RC1 和更新版本中,當專案使用目標 Framework Moniker (例如 net5.0-windows) 來鎖定 .NET 5 的 OS 特定變體時,組建便會新增組件等級的 SupportedOSPlatformAttribute 屬性。。

變更原因

.NET 5 Preview 8 在 System.Runtime.Versioning 引入了屬性來為 API 指定支援的平台。 平台相容性分析器 會耗用這些屬性,在不支援這些 API 的平台耗用平台特定的 API 時產生組建警告。

對於 .NET 5 RC1,已在平台相容性分析器中新增了一個額外的功能以進行平台排除。 這項功能可在 OS 平台上將 API 標示為完全不受支援。 此功能也會提示變更屬性,包括使用更合適的名稱。 之所以移除 ObsoletedInOSPlatformAttribute,是因為已經不需要此屬性。

導入的版本

5.0 RC1

當您將專案的目標框架從 .NET 5 Preview 8 重定為 .NET 5 RC1 時,您可能會因這些變更而遇到組建或執行階段錯誤。 例如,重新命名 MinimumOSPlatformAttribute 很可能造成錯誤,因為屬性在組建階段套用至平台特定的組件,而舊的組建成品仍參考舊的 API 名稱。

組建階段錯誤範例:

  • 錯誤 CS0246:找不到名為 'MinimumOSPlatformAttribute' 的型別或命名空間名稱 (是否遺漏 using 指示詞或組件參考?)
  • 錯誤 CS0246:找不到名為 'RemovedInOSPlatformAttribute' 的型別或命名空間名稱 (是否遺漏 using 指示詞或組件參考?)
  • 錯誤 CS0246:找不到名為 'ObsoletedInOSPlatformAttribute' 的型別或命名空間名稱 (是否遺漏 using 指示詞或組件參考?)

執行階段錯誤範例:

未處理的例外狀況。 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