SYSLIB1011:日誌方法無法使用該 allows ref struct 限制

使用 LoggerMessageAttribute 註解的方法會對型別參數使用 C# 13 allows ref struct 反約束。 日誌原始碼產生器會把參數存在結構體欄位,所以無法保留 ref struct 型別參數。

因應措施

日誌方法支援通用型別參數及大多數約束形式(class、structunmanagednotnullnew()基型、介面及)。 移除 allows ref struct 型別參數中的反約束以解決此錯誤。

隱藏警告

建議您盡可能使用其中一種因應措施。 不過,如果您無法變更程式碼,則可以透過#pragma指令或專案設定來隱藏<NoWarn>警告。 如果來源產生器診斷未顯示為錯誤,您可以在 SYSLIB1XXX 程式碼或專案檔中隱藏警告。

要在程式碼中抑制警告(必要時替換診斷 ID):

// Disable the warning.
#pragma warning disable SYSLIB1006

// Code that generates compiler diagnostic.
// ...

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

要在專案檔案中抑制警告(必要時替換診斷 ID):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   <TargetFramework>net11.0</TargetFramework>
   <!-- NoWarn below suppresses SYSLIB1002 project-wide -->
   <NoWarn>$(NoWarn);SYSLIB1002</NoWarn>
   <!-- To suppress multiple warnings, you can use multiple NoWarn elements -->
   <NoWarn>$(NoWarn);SYSLIB1002</NoWarn>
   <NoWarn>$(NoWarn);SYSLIB1006</NoWarn>
   <!-- Alternatively, you can suppress multiple warnings by using a semicolon-delimited list -->
   <NoWarn>$(NoWarn);SYSLIB1002;SYSLIB1006;SYSLIB1007</NoWarn>
  </PropertyGroup>
</Project>