@@ -14,17 +14,15 @@ static class HttpApiMethodFinder
1414 /// 查找接口类型及其继承的接口的所有方法
1515 /// </summary>
1616 /// <param name="httpApiType">接口类型</param>
17- /// <exception cref="ArgumentException"></exception>
18- /// <exception cref="NotSupportedException"></exception>
17+ /// <exception cref="ArgumentException"></exception>
1918 /// <returns></returns>
2019 public static IEnumerable < MethodInfo > FindApiMethods ( Type httpApiType )
2120 {
2221 var interfaces = httpApiType . GetInterfaces ( ) . Append ( httpApiType ) ;
2322 return Sort ( interfaces , t => t . GetInterfaces ( ) )
2423 . 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 ) ;
2826 }
2927
3028 /// <summary>
@@ -49,8 +47,7 @@ private static IList<T> Sort<T>(IEnumerable<T> source, Func<T, IEnumerable<T>> g
4947
5048 private static void Visit < T > ( T item , Func < T , IEnumerable < T > > getDependencies , List < T > sorted , Dictionary < T , bool > visited )
5149 {
52- bool inProcess ;
53- var alreadyVisited = visited . TryGetValue ( item , out inProcess ) ;
50+ var alreadyVisited = visited . TryGetValue ( item , out var inProcess ) ;
5451
5552 // 如果已经访问该顶点,则直接返回
5653 if ( alreadyVisited )
@@ -85,33 +82,14 @@ private static void Visit<T>(T item, Func<T, IEnumerable<T>> getDependencies, Li
8582 }
8683
8784 /// <summary>
88- /// 表示MethodInfo的特征
85+ /// 表示MethodInfo的相等比较器
8986 /// </summary>
90- private class MethodFeature : IEquatable < MethodFeature >
87+ private class MethodEqualityComparer : IEqualityComparer < MethodInfo >
9188 {
92- private readonly MethodInfo method ;
89+ public static MethodEqualityComparer Default { get ; } = new MethodEqualityComparer ( ) ;
9390
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 )
11192 {
112- var x = this . method ;
113- var y = other . method ;
114-
11593 if ( x . Name != y . Name || x . ReturnType != y . ReturnType )
11694 {
11795 return false ;
@@ -122,26 +100,17 @@ public bool Equals(MethodFeature other)
122100 return xParameterTypes . SequenceEqual ( yParameterTypes ) ;
123101 }
124102
125- /// <summary>
126- /// 获取哈希
127- /// </summary>
128- /// <returns></returns>
129- public override int GetHashCode ( )
103+ public int GetHashCode ( MethodInfo obj )
130104 {
131105 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 ( ) )
135109 {
136110 hashCode . Add ( parameter . ParameterType ) ;
137111 }
138112 return hashCode . ToHashCode ( ) ;
139113 }
140-
141- public override bool Equals ( object obj )
142- {
143- return obj is MethodFeature other && this . Equals ( other ) ;
144- }
145114 }
146115 }
147116}
0 commit comments