refactor(GFramework.SourceGenerators): 优化集合类型候选枚举逻辑

- 将嵌套循环重构为使用 SelectMany 扁平化集合类型参数
- 简化了候选元素类型的遍历逻辑
- 提高了代码可读性和性能
- 移除了不必要的 continue 语句
- 保持了原有的类型匹配功能不变
This commit is contained in:
GeWuYou 2026-03-28 19:40:02 +08:00
parent d72d4ab0bd
commit 4fb1da2da6

View File

@ -711,11 +711,11 @@ public sealed class ContextGetGenerator : IIncrementalGenerator
if (readOnlyList is null || fieldType is not INamedTypeSymbol targetType) if (readOnlyList is null || fieldType is not INamedTypeSymbol targetType)
return false; return false;
foreach (var candidateType in EnumerateCollectionTypeCandidates(targetType)) var allTypeCandidates = EnumerateCollectionTypeCandidates(targetType)
{ .SelectMany(candidateType => candidateType.TypeArguments);
if (candidateType is not { TypeArguments: [var candidateElementType] })
continue;
foreach (var candidateElementType in allTypeCandidates)
{
var expectedSourceType = readOnlyList.Construct(candidateElementType); var expectedSourceType = readOnlyList.Construct(candidateElementType);
if (!expectedSourceType.IsAssignableTo(targetType)) if (!expectedSourceType.IsAssignableTo(targetType))
continue; continue;
@ -724,10 +724,10 @@ public sealed class ContextGetGenerator : IIncrementalGenerator
return true; return true;
} }
return false; return false;
} }
private static IEnumerable<INamedTypeSymbol> EnumerateCollectionTypeCandidates(INamedTypeSymbol typeSymbol) private static IEnumerable<INamedTypeSymbol> EnumerateCollectionTypeCandidates(INamedTypeSymbol typeSymbol)
{ {
yield return typeSymbol; yield return typeSymbol;