|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
2 | 4 | using System.Linq;
|
3 | 5 | using System.Reflection;
|
4 | 6 | using WebApiClientCore.Exceptions;
|
@@ -68,31 +70,35 @@ public THttpApi CreateInstance(IHttpApiInterceptor apiInterceptor)
|
68 | 70 | /// <param name="httpApiType">接口类型</param>
|
69 | 71 | /// <param name="proxyClassType">接口的实现类型</param>
|
70 | 72 | /// <returns></returns>
|
71 |
| - private static MethodInfo[] FindApiMethods(Type httpApiType, Type proxyClassType) |
| 73 | + private static IEnumerable<MethodInfo> FindApiMethods(Type httpApiType, Type proxyClassType) |
72 | 74 | {
|
73 |
| - var apiMethods = HttpApi.FindApiMethods(httpApiType); |
74 |
| - var classMethods = proxyClassType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance); |
| 75 | + var apiMethods = HttpApi.FindApiMethods(httpApiType) |
| 76 | + .Select(item => new MethodFeature(item, isProxyMethod: false)) |
| 77 | + .ToArray(); |
75 | 78 |
|
76 |
| - // 按照Index特征对apiMethods进行排序 |
77 |
| - var query = from a in apiMethods.Select(item => new MethodFeature(item, isProxyMethod: false)) |
78 |
| - join c in classMethods.Select(item => new MethodFeature(item, isProxyMethod: true)) |
79 |
| - on a equals c |
80 |
| - orderby c.Index |
81 |
| - select a.Method; |
82 |
| - |
83 |
| - var methods = query.ToArray(); |
84 |
| - if (apiMethods.Length != methods.Length) |
| 79 | + var classMethods = proxyClassType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) |
| 80 | + .Select(item => new MethodFeature(item, isProxyMethod: true)) |
| 81 | + .Where(item => item.Index >= 0) |
| 82 | + .ToArray(); |
| 83 | + |
| 84 | + if (apiMethods.Length != classMethods.Length) |
85 | 85 | {
|
86 |
| - var missingMethod = apiMethods.Except(methods).FirstOrDefault(); |
87 |
| - var message = $"{httpApiType}的代理类缺失方法{missingMethod}"; |
| 86 | + var message = $"接口类型{httpApiType}与其代理类不匹配,请重新编译接口类型所在的项目"; |
88 | 87 | throw new ProxyTypeException(httpApiType, message);
|
89 | 88 | }
|
90 |
| - return methods; |
| 89 | + |
| 90 | + // 按照Index特征对apiMethods进行排序 |
| 91 | + return from a in apiMethods |
| 92 | + join c in classMethods |
| 93 | + on a equals c |
| 94 | + orderby c.Index |
| 95 | + select a.Method; |
91 | 96 | }
|
92 | 97 |
|
93 | 98 | /// <summary>
|
94 | 99 | /// 表示MethodInfo的特征
|
95 | 100 | /// </summary>
|
| 101 | + [DebuggerDisplay("[{Index,nq}] {declaringType.FullName,nq}.{name,nq}")] |
96 | 102 | private sealed class MethodFeature : IEquatable<MethodFeature>
|
97 | 103 | {
|
98 | 104 | private readonly string name;
|
|
0 commit comments