Skip to content

Commit 3a2c9da

Browse files
committed
方法查找结果严格检查
1 parent f56deac commit 3a2c9da

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

WebApiClientCore/Exceptions/ProxyTypeCreateException.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@ namespace WebApiClientCore.Exceptions
55
/// <summary>
66
/// 表示代理类创建异常
77
/// </summary>
8-
public class ProxyTypeCreateException : Exception
8+
public class ProxyTypeCreateException : ProxyTypeException
99
{
10-
/// <summary>
11-
/// 接口类型
12-
/// </summary>
13-
public Type InterfaceType { get; }
14-
1510
/// <summary>
1611
/// 代理类创建异常
1712
/// </summary>
1813
/// <param name="interfaceType">接口类型</param>
1914
public ProxyTypeCreateException(Type interfaceType)
20-
: this(interfaceType, null)
15+
: base(interfaceType)
2116
{
2217
}
2318

@@ -27,9 +22,8 @@ public ProxyTypeCreateException(Type interfaceType)
2722
/// <param name="interfaceType">接口类型</param>
2823
/// <param name="message">提示消息</param>
2924
public ProxyTypeCreateException(Type interfaceType, string? message)
30-
: base(message)
25+
: base(interfaceType, message)
3126
{
32-
this.InterfaceType = interfaceType;
3327
}
3428
}
3529
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace WebApiClientCore.Exceptions
4+
{
5+
/// <summary>
6+
/// 表示代理类异常
7+
/// </summary>
8+
public class ProxyTypeException : Exception
9+
{
10+
/// <summary>
11+
/// 接口类型
12+
/// </summary>
13+
public Type InterfaceType { get; }
14+
15+
/// <summary>
16+
/// 代理类异常
17+
/// </summary>
18+
/// <param name="interfaceType">接口类型</param>
19+
public ProxyTypeException(Type interfaceType)
20+
: this(interfaceType, null)
21+
{
22+
}
23+
24+
/// <summary>
25+
/// 代理类创建异常
26+
/// </summary>
27+
/// <param name="interfaceType">接口类型</param>
28+
/// <param name="message">提示消息</param>
29+
public ProxyTypeException(Type interfaceType, string? message)
30+
: base(message)
31+
{
32+
this.InterfaceType = interfaceType;
33+
}
34+
}
35+
}

WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,21 @@ private static MethodInfo[] FindApiMethods(Type httpApiType, Type proxyClassType
7373
var apiMethods = HttpApi.FindApiMethods(httpApiType);
7474
var classMethods = proxyClassType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
7575

76-
var methods = from a in apiMethods.Select(item => new MethodFeature(item))
77-
join c in classMethods.Select(item => new MethodFeature(item))
78-
on a equals c
79-
orderby c.Index
80-
select a.Method;
81-
82-
return methods.ToArray();
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)
85+
{
86+
var missingMethod = apiMethods.Except(methods).FirstOrDefault();
87+
var message = $"{httpApiType}的代理类缺失方法{missingMethod}";
88+
throw new ProxyTypeException(httpApiType, message);
89+
}
90+
return methods;
8391
}
8492

8593
/// <summary>
@@ -97,10 +105,17 @@ private sealed class MethodFeature : IEquatable<MethodFeature>
97105
/// MethodInfo的特征
98106
/// </summary>
99107
/// <param name="method"></param>
100-
public MethodFeature(MethodInfo method)
108+
/// <param name="isProxyMethod"></param>
109+
public MethodFeature(MethodInfo method, bool isProxyMethod)
101110
{
102-
var attribute = method.GetCustomAttribute<HttpApiProxyMethodAttribute>();
103111
this.Method = method;
112+
113+
var attribute = default(HttpApiProxyMethodAttribute);
114+
if (isProxyMethod)
115+
{
116+
attribute = method.GetCustomAttribute<HttpApiProxyMethodAttribute>();
117+
}
118+
104119
this.Index = attribute == null ? -1 : attribute.Index;
105120
this.Name = attribute == null ? $"{method.DeclaringType?.FullName}.{method.Name}" : attribute.Name;
106121
}

0 commit comments

Comments
 (0)