Skip to content

Commit 8b84d6d

Browse files
committed
显式实现接口
1 parent aa56411 commit 8b84d6d

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

WebApiClientCore.Analyzers/SourceGenerator/HttpApiProxyClass.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,41 +103,32 @@ public override string ToString()
103103
builder.AppendLine();
104104

105105
var index = 0;
106-
foreach (var method in this.FindApiMethods())
106+
foreach (var interfaceType in this.httpApi.AllInterfaces.Append(httpApi))
107107
{
108-
var methodCode = this.BuildMethod(method, index);
109-
builder.AppendLine(methodCode);
110-
index += 1;
108+
foreach (var method in interfaceType.GetMembers().OfType<IMethodSymbol>())
109+
{
110+
var methodCode = this.BuildMethod(interfaceType, method, index);
111+
builder.AppendLine(methodCode);
112+
index += 1;
113+
}
111114
}
112115

116+
113117
builder.AppendLine("\t}");
114118
builder.AppendLine("}");
115119
builder.AppendLine("#pragma warning restore");
116120

117121
return builder.ToString();
118122
}
119123

120-
/// <summary>
121-
/// 查找接口类型及其继承的接口的所有方法
122-
/// </summary>
123-
/// <returns></returns>
124-
private IEnumerable<IMethodSymbol> FindApiMethods()
125-
{
126-
#pragma warning disable RS1024
127-
return this.httpApi.AllInterfaces.Append(httpApi)
128-
.SelectMany(item => item.GetMembers())
129-
.OfType<IMethodSymbol>()
130-
.Distinct(MethodEqualityComparer.Default);
131-
#pragma warning restore RS1024
132-
}
133-
134124
/// <summary>
135125
/// 构建方法
136126
/// </summary>
127+
/// <param name="interfaceType"></param>
137128
/// <param name="method"></param>
138129
/// <param name="index"></param>
139130
/// <returns></returns>
140-
private string BuildMethod(IMethodSymbol method, int index)
131+
private string BuildMethod(INamedTypeSymbol interfaceType, IMethodSymbol method, int index)
141132
{
142133
var builder = new StringBuilder();
143134
var parametersString = string.Join(",", method.Parameters.Select(item => $"{GetFullName(item.Type)} {item.Name}"));
@@ -146,9 +137,10 @@ private string BuildMethod(IMethodSymbol method, int index)
146137
? "global::System.Array.Empty<global::System.Object>()"
147138
: $"new global::System.Object[] {{ {parameterNamesString} }}";
148139

140+
var methodName = $"\"{interfaceType.ToDisplayString()}.{method.Name}\"";
149141
var returnTypeString = GetFullName(method.ReturnType);
150-
builder.AppendLine($"\t\t[global::WebApiClientCore.HttpApiProxyMethod({index})]");
151-
builder.AppendLine($"\t\tpublic {returnTypeString} {method.Name}( {parametersString} )");
142+
builder.AppendLine($"\t\t[global::WebApiClientCore.HttpApiProxyMethod({index}, {methodName})]");
143+
builder.AppendLine($"\t\t{returnTypeString} {GetFullName(interfaceType)}.{method.Name}( {parametersString} )");
152144
builder.AppendLine("\t\t{");
153145
builder.AppendLine($"\t\t\treturn ({returnTypeString})this.{this.apiInterceptorFieldName}.Intercept(this.{this.actionInvokersFieldName}[{index}], {paremterArrayString});");
154146
builder.AppendLine("\t\t}");

WebApiClientCore/HttpApiMethodFinder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static IEnumerable<MethodInfo> FindApiMethods(Type httpApiType)
2222
return Sort(interfaces, t => t.GetInterfaces())
2323
.Reverse()
2424
.SelectMany(item => item.GetMethods())
25-
.Distinct(MethodEqualityComparer.Default);
25+
//.Distinct(MethodEqualityComparer.Default)
26+
;
2627
}
2728

2829
/// <summary>

WebApiClientCore/HttpApiProxyMethodAttribute.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ public sealed class HttpApiProxyMethodAttribute : Attribute
1515
/// </summary>
1616
public int Index { get; }
1717

18+
/// <summary>
19+
/// 获取名称
20+
/// </summary>
21+
public string Name { get; }
22+
1823
/// <summary>
1924
/// 方法的索引特性
2025
/// </summary>
2126
/// <param name="index">索引值,确保连续且不重复</param>
22-
public HttpApiProxyMethodAttribute(int index)
27+
/// <param name="name">方法的名称</param>
28+
public HttpApiProxyMethodAttribute(int index, string name)
2329
{
2430
this.Index = index;
31+
this.Name = name;
2532
}
2633
}
2734
}

WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ public THttpApi CreateInstance(IHttpApiInterceptor apiInterceptor)
6868
private static MethodInfo[] FindApiMethods(Type proxyType)
6969
{
7070
var apiMethods = HttpApi.FindApiMethods(typeof(THttpApi));
71-
var proxyMethods = proxyType.GetMethods();
71+
var proxyMethods = proxyType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
7272

73-
var methods = from a in apiMethods
74-
join p in proxyMethods
75-
on new MethodFeature(a) equals new MethodFeature(p)
76-
let methodAttr = p.GetCustomAttribute<HttpApiProxyMethodAttribute>()
77-
where methodAttr != null
78-
orderby methodAttr.Index
79-
select a;
73+
var methods = from a in apiMethods.Select(item => new MethodFeature(item))
74+
join p in proxyMethods.Select(item => new MethodFeature(item))
75+
on a equals p
76+
orderby p.Index
77+
select a.Method;
8078

8179
return methods.ToArray();
8280
}
@@ -112,15 +110,22 @@ orderby methodAttr.Index
112110
/// </summary>
113111
private sealed class MethodFeature : IEquatable<MethodFeature>
114112
{
115-
private readonly MethodInfo method;
113+
public MethodInfo Method { get; }
114+
115+
public int Index { get; }
116+
117+
public string Name { get; }
116118

117119
/// <summary>
118120
/// MethodInfo的特征
119121
/// </summary>
120122
/// <param name="method"></param>
121123
public MethodFeature(MethodInfo method)
122124
{
123-
this.method = method;
125+
var attribute = method.GetCustomAttribute<HttpApiProxyMethodAttribute>();
126+
this.Method = method;
127+
this.Index = attribute == null ? -1 : attribute.Index;
128+
this.Name = attribute == null ? $"{method.DeclaringType?.FullName}.{method.Name}" : attribute.Name;
124129
}
125130

126131
/// <summary>
@@ -130,15 +135,14 @@ public MethodFeature(MethodInfo method)
130135
/// <returns></returns>
131136
public bool Equals(MethodFeature? other)
132137
{
133-
if (other == null)
138+
if (other == null || this.Name != other.Name)
134139
{
135140
return false;
136141
}
137142

138-
var x = this.method;
139-
var y = other.method;
140-
141-
if (x.Name != y.Name || x.ReturnType != y.ReturnType)
143+
var x = this.Method;
144+
var y = other.Method;
145+
if (x.ReturnType != y.ReturnType)
142146
{
143147
return false;
144148
}
@@ -161,9 +165,9 @@ public override bool Equals(object? obj)
161165
public override int GetHashCode()
162166
{
163167
var hashCode = new HashCode();
164-
hashCode.Add(this.method.Name);
165-
hashCode.Add(this.method.ReturnType);
166-
foreach (var parameter in this.method.GetParameters())
168+
hashCode.Add(this.Name);
169+
hashCode.Add(this.Method.ReturnType);
170+
foreach (var parameter in this.Method.GetParameters())
167171
{
168172
hashCode.Add(parameter.ParameterType);
169173
}

0 commit comments

Comments
 (0)