Skip to content

Commit 896b26b

Browse files
committed
修复并行查找代理类时不正确的问题
1 parent c98be11 commit 896b26b

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed
Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43
using System.Reflection;
54

@@ -12,7 +11,7 @@ static class SourceGeneratorProxyClassType
1211
{
1312
private static readonly object syncRoot = new();
1413
private static readonly HashSet<Assembly> assemblies = [];
15-
private static readonly ConcurrentDictionary<Type, Type> httpApiProxyClassTable = [];
14+
private static readonly Dictionary<Type, Type> httpApiProxyClassTable = [];
1615
private const string HttpApiProxyClassTypeName = "WebApiClientCore.HttpApiProxyClass";
1716

1817
/// <summary>
@@ -22,7 +21,13 @@ static class SourceGeneratorProxyClassType
2221
/// <returns></returns>
2322
public static Type? Find(Type httpApiType)
2423
{
25-
AnalyzeAssembly(httpApiType.Assembly);
24+
lock (syncRoot)
25+
{
26+
if (assemblies.Add(httpApiType.Assembly))
27+
{
28+
AnalyzeAssembly(httpApiType.Assembly);
29+
}
30+
}
2631

2732
if (httpApiProxyClassTable.TryGetValue(httpApiType, out var proxyClassType))
2833
{
@@ -34,32 +39,18 @@ static class SourceGeneratorProxyClassType
3439

3540
private static void AnalyzeAssembly(Assembly assembly)
3641
{
37-
if (AddAssembly(assembly))
42+
var httpApiProxyClass = assembly.GetType(HttpApiProxyClassTypeName);
43+
if (httpApiProxyClass != null)
3844
{
39-
var httpApiProxyClass = assembly.GetType(HttpApiProxyClassTypeName);
40-
if (httpApiProxyClass != null)
45+
foreach (var classType in httpApiProxyClass.GetNestedTypes(BindingFlags.NonPublic))
4146
{
42-
foreach (var classType in httpApiProxyClass.GetNestedTypes(BindingFlags.NonPublic))
47+
var proxyClassAttr = classType.GetCustomAttribute<HttpApiProxyClassAttribute>();
48+
if (proxyClassAttr != null && proxyClassAttr.HttpApiType.IsAssignableFrom(classType))
4349
{
44-
if (classType.IsClass)
45-
{
46-
var proxyClassAttr = classType.GetCustomAttribute<HttpApiProxyClassAttribute>();
47-
if (proxyClassAttr != null && proxyClassAttr.HttpApiType.IsAssignableFrom(classType))
48-
{
49-
httpApiProxyClassTable.TryAdd(proxyClassAttr.HttpApiType, classType);
50-
}
51-
}
50+
httpApiProxyClassTable.TryAdd(proxyClassAttr.HttpApiType, classType);
5251
}
5352
}
5453
}
5554
}
56-
57-
private static bool AddAssembly(Assembly assembly)
58-
{
59-
lock (syncRoot)
60-
{
61-
return assemblies.Add(assembly);
62-
}
63-
}
6455
}
6556
}

0 commit comments

Comments
 (0)