非通用 JsonStringEnumConverter 版需要動態程式碼,無法與原始碼產生一起使用。
因應措施
使用 JsonStringEnumConverter<TEnum>,這樣不需要運行時代碼生成。
隱藏警告
建議您盡可能使用其中一種因應措施。 不過,如果您無法變更程式碼,則可以透過#pragma指令或專案設定來隱藏<NoWarn>警告。 如果來源產生器診斷未顯示為錯誤,您可以在 SYSLIB1XXX 程式碼或專案檔中隱藏警告。
若要隱藏程式碼中的警告:
// Disable the warning.
#pragma warning disable SYSLIB1006
// Code that generates compiler diagnostic.
// ...
// Re-enable the warning.
#pragma warning restore SYSLIB1006
若要隱藏專案檔中的警告:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.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>