Skip to content

Commit 1ecb41b

Browse files
committed
api数量严格比较
1 parent a542731 commit 1ecb41b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

WebApiClientCore.Analyzers/SourceGenerator/HttpApiProxyClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public override string ToString()
8989
builder.AppendLine($"\t[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]");
9090
builder.AppendLine($"\t[global::System.Diagnostics.DebuggerTypeProxy(typeof({this.httpApiFullName}))]");
9191
builder.AppendLine($"\t[global::WebApiClientCore.HttpApiProxyClass(typeof({this.httpApiFullName}))]");
92-
builder.AppendLine($"\tpartial class {this.ClassName} : {this.httpApiFullName}");
92+
builder.AppendLine($"\tsealed class {this.ClassName} : {this.httpApiFullName}");
9393
builder.AppendLine("\t{");
9494

9595
builder.AppendLine($"\t\tprivate readonly global::WebApiClientCore.IHttpApiInterceptor {this.apiInterceptorFieldName};");

WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Linq;
35
using System.Reflection;
46
using WebApiClientCore.Exceptions;
@@ -68,31 +70,35 @@ public THttpApi CreateInstance(IHttpApiInterceptor apiInterceptor)
6870
/// <param name="httpApiType">接口类型</param>
6971
/// <param name="proxyClassType">接口的实现类型</param>
7072
/// <returns></returns>
71-
private static MethodInfo[] FindApiMethods(Type httpApiType, Type proxyClassType)
73+
private static IEnumerable<MethodInfo> FindApiMethods(Type httpApiType, Type proxyClassType)
7274
{
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();
7578

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)
8585
{
86-
var missingMethod = apiMethods.Except(methods).FirstOrDefault();
87-
var message = $"{httpApiType}的代理类缺失方法{missingMethod}";
86+
var message = $"接口类型{httpApiType}与其代理类不匹配,请重新编译接口类型所在的项目";
8887
throw new ProxyTypeException(httpApiType, message);
8988
}
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;
9196
}
9297

9398
/// <summary>
9499
/// 表示MethodInfo的特征
95100
/// </summary>
101+
[DebuggerDisplay("[{Index,nq}] {declaringType.FullName,nq}.{name,nq}")]
96102
private sealed class MethodFeature : IEquatable<MethodFeature>
97103
{
98104
private readonly string name;

0 commit comments

Comments
 (0)