refactor(GFramework.SourceGenerators): 优化ContextGetGenerator中的集合类型遍历逻辑

- 重构了EnumerateCollectionTypeCandidates方法的调用方式
- 使用模式匹配简化了类型参数的提取逻辑
- 移除了不必要的SelectMany操作提高代码可读性
- 添加了空值检查增强代码健壮性
- 在方法末尾添加了必要的换行符保持代码格式一致
This commit is contained in:
GeWuYou 2026-03-28 19:32:23 +08:00
parent d1eafe2c9b
commit d72d4ab0bd

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;
var allTypeCandidates = EnumerateCollectionTypeCandidates(targetType) foreach (var candidateType in EnumerateCollectionTypeCandidates(targetType))
.SelectMany(candidateType => candidateType.TypeArguments);
foreach (var candidateElementType in allTypeCandidates)
{ {
if (candidateType is not { TypeArguments: [var candidateElementType] })
continue;
var expectedSourceType = readOnlyList.Construct(candidateElementType); var expectedSourceType = readOnlyList.Construct(candidateElementType);
if (!expectedSourceType.IsAssignableTo(targetType)) if (!expectedSourceType.IsAssignableTo(targetType))
continue; continue;
@ -727,6 +727,7 @@ public sealed class ContextGetGenerator : IIncrementalGenerator
return false; return false;
} }
private static IEnumerable<INamedTypeSymbol> EnumerateCollectionTypeCandidates(INamedTypeSymbol typeSymbol) private static IEnumerable<INamedTypeSymbol> EnumerateCollectionTypeCandidates(INamedTypeSymbol typeSymbol)
{ {
yield return typeSymbol; yield return typeSymbol;