Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1cf3963

Browse files
committedJun 7, 2024·
HttpApiProxyClass代码优化
1 parent a6835e3 commit 1cf3963

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed
 

‎WebApiClientCore.Analyzers/SourceGenerator/HttpApiProxyClass.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace WebApiClientCore.Analyzers.SourceGenerator
99
{
1010
/// <summary>
11-
/// HttpApi代理类
11+
/// HttpApi代理类生成器
1212
/// </summary>
1313
sealed class HttpApiProxyClass : IEquatable<HttpApiProxyClass>
1414
{
@@ -25,7 +25,7 @@ sealed class HttpApiProxyClass : IEquatable<HttpApiProxyClass>
2525
public string FileName { get; }
2626

2727
/// <summary>
28-
/// HttpApi代理类
28+
/// HttpApi代理类生成器
2929
/// </summary>
3030
/// <param name="httpApi"></param>
3131
public HttpApiProxyClass(INamedTypeSymbol httpApi)
@@ -70,7 +70,7 @@ public override string ToString()
7070
builder.AppendLine("\t{");
7171
builder.AppendLine($"\t\t[global::WebApiClientCore.HttpApiProxyClass(typeof({this.httpApiFullName}))]");
7272
builder.AppendLine($"\t\t[global::System.Diagnostics.DebuggerTypeProxy(typeof({this.httpApiFullName}))]");
73-
builder.AppendLine($"\t\tsealed class {this.proxyClassName} : {this.httpApiFullName}");
73+
builder.AppendLine($"\t\tsealed partial class {this.proxyClassName} : {this.httpApiFullName}");
7474
builder.AppendLine("\t\t{");
7575

7676
builder.AppendLine($"\t\t\tprivate readonly global::WebApiClientCore.IHttpApiInterceptor {ApiInterceptorFieldName};");
@@ -84,11 +84,11 @@ public override string ToString()
8484
builder.AppendLine();
8585

8686
var index = 0;
87-
foreach (var interfaceType in this.httpApi.AllInterfaces.Append(httpApi))
87+
foreach (var declaringType in this.httpApi.AllInterfaces.Append(httpApi))
8888
{
89-
foreach (var method in interfaceType.GetMembers().OfType<IMethodSymbol>())
89+
foreach (var method in declaringType.GetMembers().OfType<IMethodSymbol>())
9090
{
91-
var methodCode = this.BuildMethod(interfaceType, method, index);
91+
var methodCode = this.BuildMethod(declaringType, method, index);
9292
builder.AppendLine(methodCode);
9393
index += 1;
9494
}
@@ -105,11 +105,11 @@ public override string ToString()
105105
/// <summary>
106106
/// 构建方法
107107
/// </summary>
108-
/// <param name="interfaceType"></param>
108+
/// <param name="declaringType"></param>
109109
/// <param name="method"></param>
110110
/// <param name="index"></param>
111111
/// <returns></returns>
112-
private string BuildMethod(INamedTypeSymbol interfaceType, IMethodSymbol method, int index)
112+
private string BuildMethod(INamedTypeSymbol declaringType, IMethodSymbol method, int index)
113113
{
114114
var builder = new StringBuilder();
115115
var parametersString = string.Join(",", method.Parameters.Select(item => $"{GetFullName(item.Type)} {item.Name}"));
@@ -119,8 +119,10 @@ private string BuildMethod(INamedTypeSymbol interfaceType, IMethodSymbol method,
119119
: $"new global::System.Object[] {{ {parameterNamesString} }}";
120120

121121
var returnTypeString = GetFullName(method.ReturnType);
122-
builder.AppendLine($"\t\t\t[global::WebApiClientCore.HttpApiProxyMethod({index}, \"{method.Name}\", typeof({GetFullName(interfaceType)}))]");
123-
builder.AppendLine($"\t\t\t{returnTypeString} {GetFullName(interfaceType)}.{method.Name}({parametersString})");
122+
var declaringTypeString = GetFullName(declaringType);
123+
124+
builder.AppendLine($"\t\t\t[global::WebApiClientCore.HttpApiProxyMethod({index}, \"{method.Name}\", typeof({declaringTypeString}))]");
125+
builder.AppendLine($"\t\t\t{returnTypeString} {declaringTypeString}.{method.Name}({parametersString})");
124126
builder.AppendLine("\t\t\t{");
125127
builder.AppendLine($"\t\t\t\treturn ({returnTypeString})this.{ApiInterceptorFieldName}.Intercept(this.{ActionInvokersFieldName}[{index}], {parameterArrayString});");
126128
builder.AppendLine("\t\t\t}");
@@ -132,23 +134,19 @@ private string BuildMethod(INamedTypeSymbol interfaceType, IMethodSymbol method,
132134
/// </summary>
133135
/// <param name="other"></param>
134136
/// <returns></returns>
135-
public bool Equals(HttpApiProxyClass other)
137+
public bool Equals(HttpApiProxyClass? other)
136138
{
137-
return this.FileName == other.FileName;
139+
return other != null && this.FileName == other.FileName;
138140
}
139141

140142
/// <summary>
141143
/// 是否与目标相等
142144
/// </summary>
143145
/// <param name="obj"></param>
144146
/// <returns></returns>
145-
public override bool Equals(object obj)
147+
public override bool Equals(object? obj)
146148
{
147-
if (obj is HttpApiProxyClass builder)
148-
{
149-
return this.Equals(builder);
150-
}
151-
return false;
149+
return obj is HttpApiProxyClass other && this.Equals(other);
152150
}
153151

154152
/// <summary>

0 commit comments

Comments
 (0)
Please sign in to comment.