From 4fb1da2da608941d226b8959809902b69c9a8603 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 28 Mar 2026 19:40:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(GFramework.SourceGenerators):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=9B=86=E5=90=88=E7=B1=BB=E5=9E=8B=E5=80=99?= =?UTF-8?q?=E9=80=89=E6=9E=9A=E4=B8=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将嵌套循环重构为使用 SelectMany 扁平化集合类型参数 - 简化了候选元素类型的遍历逻辑 - 提高了代码可读性和性能 - 移除了不必要的 continue 语句 - 保持了原有的类型匹配功能不变 --- .../Rule/ContextGetGenerator.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs b/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs index 9248f81..4186e52 100644 --- a/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs +++ b/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs @@ -711,11 +711,11 @@ public sealed class ContextGetGenerator : IIncrementalGenerator if (readOnlyList is null || fieldType is not INamedTypeSymbol targetType) return false; - foreach (var candidateType in EnumerateCollectionTypeCandidates(targetType)) - { - if (candidateType is not { TypeArguments: [var candidateElementType] }) - continue; + var allTypeCandidates = EnumerateCollectionTypeCandidates(targetType) + .SelectMany(candidateType => candidateType.TypeArguments); + foreach (var candidateElementType in allTypeCandidates) + { var expectedSourceType = readOnlyList.Construct(candidateElementType); if (!expectedSourceType.IsAssignableTo(targetType)) continue; @@ -724,10 +724,10 @@ public sealed class ContextGetGenerator : IIncrementalGenerator return true; } + return false; } - private static IEnumerable EnumerateCollectionTypeCandidates(INamedTypeSymbol typeSymbol) { yield return typeSymbol;