SYSLIB0056:採用 AssemblyHashAlgorithm 的 Assembly.LoadFrom 已淘汰

從 .NET 9 開始,採用 AssemblyHashAlgorithmAssembly.LoadFrom 多載已淘汰。 在程式碼中呼叫它會在編譯時間產生 SYSLIB0056

淘汰的原因

Assembly.LoadFrom(String, Byte[], AssemblyHashAlgorithm) 無條件擲回 NotSupportedException。 這是一個糟糕的開發體驗。 在使用之前,多載看起來像是一個有效的 API,並且在執行時會擲回。 將其標記為淘汰會在設計時提供必要的訊號,表示不應使用它。

因應措施

使用不需 AssemblyHashAlgorithmAssembly.LoadFrom 多載。

隱藏警告

若您必須使用已淘汰的 API,您可以在程式碼或專案檔中隱藏警告。

若要只隱藏單一違規,請將前置處理器指示詞新增至原始程式碼檔案,以停用並重新啟用警告。

// Disable the warning.
#pragma warning disable SYSLIB0056

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0056

若要隱藏專案中的所有 SYSLIB0056 警告,請將 <NoWarn> 屬性新增至專案檔。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0056</NoWarn>
  </PropertyGroup>
</Project>

如需詳細資訊,請參閱隱藏警告