@@ -14,17 +14,15 @@ static class HttpApiMethodFinder
14
14
/// 查找接口类型及其继承的接口的所有方法
15
15
/// </summary>
16
16
/// <param name="httpApiType">接口类型</param>
17
- /// <exception cref="ArgumentException"></exception>
18
- /// <exception cref="NotSupportedException"></exception>
17
+ /// <exception cref="ArgumentException"></exception>
19
18
/// <returns></returns>
20
19
public static IEnumerable < MethodInfo > FindApiMethods ( Type httpApiType )
21
20
{
22
21
var interfaces = httpApiType . GetInterfaces ( ) . Append ( httpApiType ) ;
23
22
return Sort ( interfaces , t => t . GetInterfaces ( ) )
24
23
. Reverse ( )
25
- . SelectMany ( item => item . GetMethods ( ) . Select ( m => new MethodFeature ( m ) ) )
26
- . Distinct ( )
27
- . Select ( item => item . Method ) ;
24
+ . SelectMany ( item => item . GetMethods ( ) )
25
+ . Distinct ( MethodEqualityComparer . Default ) ;
28
26
}
29
27
30
28
/// <summary>
@@ -49,8 +47,7 @@ private static IList<T> Sort<T>(IEnumerable<T> source, Func<T, IEnumerable<T>> g
49
47
50
48
private static void Visit < T > ( T item , Func < T , IEnumerable < T > > getDependencies , List < T > sorted , Dictionary < T , bool > visited )
51
49
{
52
- bool inProcess ;
53
- var alreadyVisited = visited . TryGetValue ( item , out inProcess ) ;
50
+ var alreadyVisited = visited . TryGetValue ( item , out var inProcess ) ;
54
51
55
52
// 如果已经访问该顶点,则直接返回
56
53
if ( alreadyVisited )
@@ -85,33 +82,14 @@ private static void Visit<T>(T item, Func<T, IEnumerable<T>> getDependencies, Li
85
82
}
86
83
87
84
/// <summary>
88
- /// 表示MethodInfo的特征
85
+ /// 表示MethodInfo的相等比较器
89
86
/// </summary>
90
- private class MethodFeature : IEquatable < MethodFeature >
87
+ private class MethodEqualityComparer : IEqualityComparer < MethodInfo >
91
88
{
92
- private readonly MethodInfo method ;
89
+ public static MethodEqualityComparer Default { get ; } = new MethodEqualityComparer ( ) ;
93
90
94
- public MethodInfo Method => method ;
95
-
96
- /// <summary>
97
- /// MethodInfo的特征
98
- /// </summary>
99
- /// <param name="method"></param>
100
- public MethodFeature ( MethodInfo method )
101
- {
102
- this . method = method ;
103
- }
104
-
105
- /// <summary>
106
- /// 比较方法原型是否相等
107
- /// </summary>
108
- /// <param name="other"></param>
109
- /// <returns></returns>
110
- public bool Equals ( MethodFeature other )
91
+ public bool Equals ( MethodInfo x , MethodInfo y )
111
92
{
112
- var x = this . method ;
113
- var y = other . method ;
114
-
115
93
if ( x . Name != y . Name || x . ReturnType != y . ReturnType )
116
94
{
117
95
return false ;
@@ -122,26 +100,17 @@ public bool Equals(MethodFeature other)
122
100
return xParameterTypes . SequenceEqual ( yParameterTypes ) ;
123
101
}
124
102
125
- /// <summary>
126
- /// 获取哈希
127
- /// </summary>
128
- /// <returns></returns>
129
- public override int GetHashCode ( )
103
+ public int GetHashCode ( MethodInfo obj )
130
104
{
131
105
var hashCode = new HashCode ( ) ;
132
- hashCode . Add ( this . method . Name ) ;
133
- hashCode . Add ( this . method . ReturnType ) ;
134
- foreach ( var parameter in this . method . GetParameters ( ) )
106
+ hashCode . Add ( obj . Name ) ;
107
+ hashCode . Add ( obj . ReturnType ) ;
108
+ foreach ( var parameter in obj . GetParameters ( ) )
135
109
{
136
110
hashCode . Add ( parameter . ParameterType ) ;
137
111
}
138
112
return hashCode . ToHashCode ( ) ;
139
113
}
140
-
141
- public override bool Equals ( object obj )
142
- {
143
- return obj is MethodFeature other && this . Equals ( other ) ;
144
- }
145
114
}
146
115
}
147
116
}
0 commit comments