如果你的 .NET 6+ 项目引用一个启用源生成代码的包(例如日志记录解决方案),则特定于源生成的分析器将在编译时运行。 本文列出了与源生成代码相关的编译器诊断。
如果你遇到其中一个生成警告或错误,请按照为参考部分列出的诊断 ID 提供的具体指导进行操作。 还可以使用特定的 SYSLIB1XXX 诊断 ID 值来禁止显示警告。 有关详细信息,请参阅取消警告。
分析器警告
为源生成代码分析器警告保留的诊断 ID 值为 SYSLIB1001 至 SYSLIB1999。
参考
下表提供了 .NET 6 及更高版本中 SYSLIB1XXX 诊断的索引。
| 诊断 ID | 说明 |
|---|---|
| SYSLIB1001 | 日志记录方法名称不能以 _ 开头 |
| SYSLIB1002 | 不要将日志级别参数作为模板包含在日志记录消息中 |
| SYSLIB1003 | InvalidLoggingMethodParameterNameTitle |
| SYSLIB1004 | 日志记录类不能为嵌套类型。 |
| SYSLIB1005 | 找不到所需的类型定义 |
| SYSLIB1006 | 多个日志记录方法不能在类中使用相同的事件 ID |
| SYSLIB1007 | 日志记录方法必须返回 void |
| SYSLIB1008 | 日志记录方法的参数之一必须实现 Microsoft.Extensions.Logging.ILogger 接口 |
| SYSLIB1009 | 日志记录方法必须为 static |
| SYSLIB1010 | 日志记录方法必须为 partial |
| SYSLIB1011 | 日志记录方法不能是泛型 |
| SYSLIB1012 | 日志记录消息中的多余限定符 |
| SYSLIB1013 | 不要将异常参数作为模板包含在日志记录消息中 |
| SYSLIB1014 | 日志记录模板无相应的方法参数 |
| SYSLIB1015 | 未从日志记录消息中引用参数 |
| SYSLIB1016 | 日志记录方法不能有主体 |
| SYSLIB1017 | 必须在 LoggerMessage 属性中提供 LogLevel 值或将其用作日志记录方法的参数 |
| SYSLIB1018 | 不要将记录器参数作为模板包含在日志记录消息中 |
| SYSLIB1019 | 找不到 Microsoft.Extensions.Logging.ILogger 类型的字段 |
| SYSLIB1020 | 找到 Microsoft.Extensions.Logging.ILogger 类型的多个字段 |
| SYSLIB1021 | 多个消息模板项名称只是大小写不同 |
| SYSLIB1022 | 不能使用格式错误的格式字符串(例如不成对的大括号) |
| SYSLIB1023 | 不支持生成六个以上的参数 |
| SYSLIB1024 | 日志记录方法自变量使用不受支持的 out 参数修饰符 |
| SYSLIB1025 | 多个日志记录方法不能在类中使用相同的事件名称 |
| SYSLIB1026 | 日志记录源生成器不支持 C# 语言版本。 |
| SYSLIB1027 | 类型 Microsoft.Extensions.Logging.ILogger 的主要构造函数参数由字段隐藏 |
| SYSLIB1028 | (已保留用于日志记录。) |
| SYSLIB1029 | (已保留用于日志记录。) |
| SYSLIB1030 |
System.Text.Json 源生成器未生成类型的序列化元数据 |
| SYSLIB1031 |
System.Text.Json 源生成器遇到重复的 JsonTypeInfo 属性名称 |
| SYSLIB1032 |
System.Text.Json 源生成器遇到非分部的上下文类 |
| SYSLIB1033 |
System.Text.Json 源生成器遇到具有多个 [JsonConstructor] 注释的类型 |
| SYSLIB1034 | JsonSourceGenerator 遇到 JsonStringEnumConverter 注释 |
| SYSLIB1035 |
System.Text.Json 源生成器遇到具有多个 [JsonExtensionData] 注释的类型 |
| SYSLIB1036 |
System.Text.Json 源生成器遇到无效的 [JsonExtensionData] 注释 |
| SYSLIB1037 |
System.Text.Json 源生成器遇到具有仅初始化属性的类型,这些属性不支持反序列化 |
| SYSLIB1038 |
System.Text.Json 源生成器遇到使用 [JsonInclude] 注释的属性,该属性具有不可访问的访问器 |
| SYSLIB1039 | JsonSourceGenerator 遇到启用了 JsonDerivedTypeAttribute 的 JsonSourceGenerationMode.Serialization 注释 |
| SYSLIB1040 | GeneratedRegexAttribute 用法无效。 |
| SYSLIB1041 | 对同一方法应用了多个 GeneratedRegexAttribute 属性,但只允许使用一个。 |
| SYSLIB1042 | 指定的正则表达式无效。 |
| SYSLIB1043 | GeneratedRegexAttribute 方法必须是分部、无参数、非泛型且非抽象的方法,并且必须返回 Regex。 |
| SYSLIB1044 | 由于内部限制,正则表达式生成器无法为指定的正则表达式生成完整的源实现。 有关更多详细信息,请参阅生成的源中的说明。 |
| SYSLIB1045 | 使用 GeneratedRegexAttribute 在编译时生成正则表达式实现。 |
| SYSLIB1046 | (已针对 System.Text.RegularExpressions.Generator 保留。) |
| SYSLIB1047 | (已针对 System.Text.RegularExpressions.Generator 保留。) |
| SYSLIB1048 | (已针对 System.Text.RegularExpressions.Generator 保留。) |
| SYSLIB1049 | (已针对 System.Text.RegularExpressions.Generator 保留。) |
| SYSLIB1050 | LibraryImportAttribute 用法无效。 |
| SYSLIB1051 | 源生成的 P/Invoke 不支持指定的类型。 |
| SYSLIB1052 | 源生成的 P/Invoke 不支持指定的配置。 |
| SYSLIB1053 | 指定的 LibraryImportAttribute 参数不能转发到 DllImportAttribute。 |
| SYSLIB1054 | 使用 LibraryImportAttribute 而不是 DllImportAttribute 在编译时生成 P/Invoke 封送代码。 |
| SYSLIB1055 | CustomMarshallerAttribute 用法无效。 |
| SYSLIB1056 | 指定的本机类型无效。 |
| SYSLIB1057 | 封送程序类型没有必需的形状。 |
| SYSLIB1058 | NativeMarshallingAttribute 用法无效。 |
| SYSLIB1059 | 封送程序类型不支持分配构造函数。 |
| SYSLIB1060 | 指定的封送程序类型无效。 |
| SYSLIB1061 | 封送程序类型具有不兼容的方法签名。 |
| SYSLIB1062 | 必须使用 <AllowUnsafeBlocks>true</AllowUnsafeBlocks> 更新项目。 |
| SYSLIB1063 | (已针对 Microsoft.Interop.LibraryImportGenerator 保留。) |
| SYSLIB1064 | (已针对 Microsoft.Interop.LibraryImportGenerator 保留。) |
| SYSLIB1065 | (已针对 Microsoft.Interop.LibraryImportGenerator 保留。) |
| SYSLIB1066 | (已针对 Microsoft.Interop.LibraryImportGenerator 保留。) |
| SYSLIB1067 | (已针对 Microsoft.Interop.LibraryImportGenerator 保留。) |
| SYSLIB1068 | (已针对 Microsoft.Interop.LibraryImportGenerator 保留。) |
| SYSLIB1069 | (已针对 Microsoft.Interop.LibraryImportGenerator 保留。) |
| SYSLIB1070 | JSImportAttribute 用法无效。 |
| SYSLIB1071 | JSExportAttribute 用法无效。 |
| SYSLIB1072 | 源生成的 JavaScript 互操作不支持指定的类型。 |
| SYSLIB1073 | 源生成的 JavaScript 互操作不支持指定的配置。 |
| SYSLIB1074 | JSImportAttribute 需要不安全的代码。 |
| SYSLIB1075 | JSImportAttribute 需要不安全的代码。 |
| SYSLIB1076 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1077 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1078 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1079 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1080 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1081 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1082 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1083 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1084 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1085 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1086 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1087 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1088 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1089 | (已针对 System.Runtime.InteropServices.JavaScript.JSImportGenerator 保留。) |
| SYSLIB1090 |
GeneratedComInterfaceAttribute 用法无效。 |
| SYSLIB1091 | 方法是采用与 GeneratedComInterface 属性不同的部分声明进行声明的。 |
| SYSLIB1092 | 使用 LibraryImport 或 GeneratedComInterface 属性不遵循建议。 |
| SYSLIB1093 | COM 接口生成分析已失败。 |
| SYSLIB1094 | 基 COM 接口无法生成源。 不会为此接口生成代码。 |
| SYSLIB1095 |
GeneratedComClassAttribute 用法无效。 |
| SYSLIB1096 | 使用 GeneratedComInterfaceAttribute 而不是 ComImportAttribute 在编译时生成 COM 封送代码。 |
| SYSLIB1097 | 此类型至少实现一个具有 GeneratedComInterfaceAttribute 特性的类型。 添加 GeneratedComClassAttribute 以支持将此类型传递给 COM,并从此类型的对象中公开带有 GeneratedComInterfaceAttribute 的类型的 COM 接口。 |
| SYSLIB1098 | 带有 EnableComHosting 的 .NET COM 承载仅支持内置 COM 互操作。 它不支持与 GeneratedComInterfaceAttribute 的源生成 COM 互操作。 |
| SYSLIB1099 | 上的 System.Runtime.InteropServices.Marshal COM 互作 API 不支持源生成的 COM,在运行时会失败。 |
| SYSLIB1100 | 配置绑定生成器:类型不受支持。 |
| SYSLIB1101 | 配置绑定生成器:不支持类型上的属性。 |
| SYSLIB1102 | 配置绑定生成器:项目的语言版本必须至少为 C# 12。 |
| SYSLIB1103 | 配置绑定生成器:值类型是配置“Bind”方法的无效输入。 |
| SYSLIB1104 | 配置绑定生成器:生成器无法确定目标配置类型。 |
| SYSLIB1105 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1106 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。)(已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1107 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1108 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1109 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1110 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1111 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1112 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1113 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1114 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1115 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1116 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1117 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1118 | (已针对 Microsoft.Extensions.Configuration.Binder.SourceGeneration 保留。) |
| SYSLIB1201 | 不能对具有开放式泛型类型的字段或属性使用 ValidateObjectMembersAttribute 或 ValidateEnumeratedItemsAttribute。 |
| SYSLIB1202 | 成员类型没有要验证的字段或属性。 |
| SYSLIB1203 | 类型没有要验证的字段或属性。 |
| SYSLIB1204 | 用 OptionsValidatorAttribute 批注的类型未实现必要的接口。 |
| SYSLIB1205 | 类型已包含“Validate”方法的实现。 |
| SYSLIB1206 | 无法验证专用字段或属性。 |
| SYSLIB1207 | 成员类型不可枚举。 |
| SYSLIB1208 | 用于可传递或可枚举验证的验证器必须具有不带参数的构造函数。 |
| SYSLIB1209 |
OptionsValidatorAttribute 不能应用于静态类。 |
| SYSLIB1210 | 为 ValidateObjectMembersAttribute 或 ValidateEnumeratedItemsAttribute 属性指定的 Null 验证器类型。 |
| SYSLIB1211 | 模型类型中不支持的循环引用。 |
| SYSLIB1212 | 成员可能缺少可传递验证。 |
| SYSLIB1213 | 成员可能缺少可枚举验证。 |
| SYSLIB1214 | 无法验证常数、静态字段或属性。 |
| SYSLIB1215 | 无法从验证器类型访问成员上的验证属性。 |
| SYSLIB1216 | 选项验证源生成器不支持 C# 语言版本。 |
| SYSLIB1217 | 验证属性仅适用于字符串、数组或 ICollection;它不能用于其他类型。 |
| SYSLIB1218 | (针对 Microsoft.Extensions.Options.SourceGeneration 保留。) |
| SYSLIB1219 | (针对 Microsoft.Extensions.Options.SourceGeneration 保留。) |
| SYSLIB1220 | JsonSourceGenerator 遇到具有无效类型参数的 [JsonConverterAttribute]。 |
| SYSLIB1221 | JsonSourceGenerator 不支持此 C# 语言版本。 |
| SYSLIB1222 | 使用 JsonConstructorAttribute 注释的构造函数不可访问。 |
| SYSLIB1223 | 使用 JsonConstructorAttribute 注释的构造函数不可访问。 |
| SYSLIB1224 | 使用 JsonSerializableAttribute 注释的类型必须是派生自 JsonSerializerContext 的类。 |
| SYSLIB1225 | (已针对 System.Text.Json.SourceGeneration 保留。) |
| SYSLIB1226 | (已针对 System.Text.Json.SourceGeneration 保留。) |
| SYSLIB1227 | (已针对 System.Text.Json.SourceGeneration 保留。) |
| SYSLIB1228 | (已针对 System.Text.Json.SourceGeneration 保留。) |
| SYSLIB1229 | (已针对 System.Text.Json.SourceGeneration 保留。) |
| SYSLIB1230 | 不支持从另一个程序集中定义的GeneratedComInterface属性接口派生。 |
禁止显示警告
建议尽量使用解决方法之一。 但是,如果无法更改代码,可以通过 #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>