File tree Expand file tree Collapse file tree 2 files changed +59
-14
lines changed
WebApiClientCore.Analyzers.SourceGenerator Expand file tree Collapse file tree 2 files changed +59
-14
lines changed Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ public override string ToString()
111
111
builder . AppendLine ( "\t \t }" ) ;
112
112
113
113
var index = 0 ;
114
- foreach ( var method in FindApiMethods ( this . httpApi ) )
114
+ foreach ( var method in HttpApiMethodFinder . FindApiMethods ( this . httpApi ) )
115
115
{
116
116
var methodCode = this . BuildMethod ( method , index ) ;
117
117
builder . AppendLine ( methodCode ) ;
@@ -125,19 +125,6 @@ public override string ToString()
125
125
return builder . ToString ( ) ;
126
126
}
127
127
128
- /// <summary>
129
- /// 查找接口类型及其继承的接口的所有方法
130
- /// </summary>
131
- /// <param name="httpApi">接口</param>
132
- /// <returns></returns>
133
- private static IEnumerable < IMethodSymbol > FindApiMethods ( INamedTypeSymbol httpApi )
134
- {
135
- return httpApi
136
- . AllInterfaces
137
- . Append ( httpApi )
138
- . SelectMany ( item => item . GetMembers ( ) )
139
- . OfType < IMethodSymbol > ( ) ;
140
- }
141
128
142
129
/// <summary>
143
130
/// 构建方法
Original file line number Diff line number Diff line change
1
+ using Microsoft . CodeAnalysis ;
2
+ using System ;
3
+ using System . Collections . Generic ;
4
+ using System . Linq ;
5
+
6
+ namespace WebApiClientCore . Analyzers . SourceGenerator
7
+ {
8
+ /// <summary>
9
+ /// HttpApi的方法查找器
10
+ /// </summary>
11
+ static class HttpApiMethodFinder
12
+ {
13
+ /// <summary>
14
+ /// 查找接口类型及其继承的接口的所有方法
15
+ /// </summary>
16
+ /// <param name="httpApiType">接口类型</param>
17
+ /// <returns></returns>
18
+ public static IEnumerable < IMethodSymbol > FindApiMethods ( INamedTypeSymbol httpApiType )
19
+ {
20
+ return httpApiType . AllInterfaces . Append ( httpApiType )
21
+ . SelectMany ( item => item . GetMembers ( ) )
22
+ . OfType < IMethodSymbol > ( )
23
+ . Distinct ( MethodEqualityComparer . Default ) ;
24
+ }
25
+
26
+ /// <summary>
27
+ /// 表示MethodInfo的相等比较器
28
+ /// </summary>
29
+ private class MethodEqualityComparer : IEqualityComparer < IMethodSymbol >
30
+ {
31
+ public static MethodEqualityComparer Default { get ; } = new MethodEqualityComparer ( ) ;
32
+
33
+ public bool Equals ( IMethodSymbol x , IMethodSymbol y )
34
+ {
35
+ if ( x . Name != y . Name || ! x . ReturnType . Equals ( y . ReturnType , SymbolEqualityComparer . Default ) )
36
+ {
37
+ return false ;
38
+ }
39
+
40
+ var xParameterTypes = x . Parameters . Select ( p => p . Type ) ;
41
+ var yParameterTypes = y . Parameters . Select ( p => p . Type ) ;
42
+ return xParameterTypes . SequenceEqual ( yParameterTypes , SymbolEqualityComparer . Default ) ;
43
+ }
44
+
45
+ public int GetHashCode ( IMethodSymbol obj )
46
+ {
47
+ var hashCode = 0 ;
48
+ hashCode ^= obj . Name . GetHashCode ( ) ;
49
+ hashCode ^= obj . ReturnType . GetHashCode ( ) ;
50
+ foreach ( var parameter in obj . Parameters )
51
+ {
52
+ hashCode ^= parameter . Type . GetHashCode ( ) ;
53
+ }
54
+ return hashCode ;
55
+ }
56
+ }
57
+ }
58
+ }
You can’t perform that action at this time.
0 commit comments