8
8
namespace WebApiClientCore . Analyzers . SourceGenerator
9
9
{
10
10
/// <summary>
11
- /// HttpApi代理类
11
+ /// HttpApi代理类生成器
12
12
/// </summary>
13
13
sealed class HttpApiProxyClass : IEquatable < HttpApiProxyClass >
14
14
{
@@ -25,7 +25,7 @@ sealed class HttpApiProxyClass : IEquatable<HttpApiProxyClass>
25
25
public string FileName { get ; }
26
26
27
27
/// <summary>
28
- /// HttpApi代理类
28
+ /// HttpApi代理类生成器
29
29
/// </summary>
30
30
/// <param name="httpApi"></param>
31
31
public HttpApiProxyClass ( INamedTypeSymbol httpApi )
@@ -70,7 +70,7 @@ public override string ToString()
70
70
builder . AppendLine ( "\t {" ) ;
71
71
builder . AppendLine ( $ "\t \t [global::WebApiClientCore.HttpApiProxyClass(typeof({ this . httpApiFullName } ))]") ;
72
72
builder . AppendLine ( $ "\t \t [global::System.Diagnostics.DebuggerTypeProxy(typeof({ this . httpApiFullName } ))]") ;
73
- builder . AppendLine ( $ "\t \t sealed class { this . proxyClassName } : { this . httpApiFullName } ") ;
73
+ builder . AppendLine ( $ "\t \t sealed partial class { this . proxyClassName } : { this . httpApiFullName } ") ;
74
74
builder . AppendLine ( "\t \t {" ) ;
75
75
76
76
builder . AppendLine ( $ "\t \t \t private readonly global::WebApiClientCore.IHttpApiInterceptor { ApiInterceptorFieldName } ;") ;
@@ -84,11 +84,11 @@ public override string ToString()
84
84
builder . AppendLine ( ) ;
85
85
86
86
var index = 0 ;
87
- foreach ( var interfaceType in this . httpApi . AllInterfaces . Append ( httpApi ) )
87
+ foreach ( var declaringType in this . httpApi . AllInterfaces . Append ( httpApi ) )
88
88
{
89
- foreach ( var method in interfaceType . GetMembers ( ) . OfType < IMethodSymbol > ( ) )
89
+ foreach ( var method in declaringType . GetMembers ( ) . OfType < IMethodSymbol > ( ) )
90
90
{
91
- var methodCode = this . BuildMethod ( interfaceType , method , index ) ;
91
+ var methodCode = this . BuildMethod ( declaringType , method , index ) ;
92
92
builder . AppendLine ( methodCode ) ;
93
93
index += 1 ;
94
94
}
@@ -105,11 +105,11 @@ public override string ToString()
105
105
/// <summary>
106
106
/// 构建方法
107
107
/// </summary>
108
- /// <param name="interfaceType "></param>
108
+ /// <param name="declaringType "></param>
109
109
/// <param name="method"></param>
110
110
/// <param name="index"></param>
111
111
/// <returns></returns>
112
- private string BuildMethod ( INamedTypeSymbol interfaceType , IMethodSymbol method , int index )
112
+ private string BuildMethod ( INamedTypeSymbol declaringType , IMethodSymbol method , int index )
113
113
{
114
114
var builder = new StringBuilder ( ) ;
115
115
var parametersString = string . Join ( "," , method . Parameters . Select ( item => $ "{ GetFullName ( item . Type ) } { item . Name } ") ) ;
@@ -119,8 +119,10 @@ private string BuildMethod(INamedTypeSymbol interfaceType, IMethodSymbol method,
119
119
: $ "new global::System.Object[] {{ { parameterNamesString } }}";
120
120
121
121
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 } )") ;
124
126
builder . AppendLine ( "\t \t \t {" ) ;
125
127
builder . AppendLine ( $ "\t \t \t \t return ({ returnTypeString } )this.{ ApiInterceptorFieldName } .Intercept(this.{ ActionInvokersFieldName } [{ index } ], { parameterArrayString } );") ;
126
128
builder . AppendLine ( "\t \t \t }" ) ;
@@ -132,23 +134,19 @@ private string BuildMethod(INamedTypeSymbol interfaceType, IMethodSymbol method,
132
134
/// </summary>
133
135
/// <param name="other"></param>
134
136
/// <returns></returns>
135
- public bool Equals ( HttpApiProxyClass other )
137
+ public bool Equals ( HttpApiProxyClass ? other )
136
138
{
137
- return this . FileName == other . FileName ;
139
+ return other != null && this . FileName == other . FileName ;
138
140
}
139
141
140
142
/// <summary>
141
143
/// 是否与目标相等
142
144
/// </summary>
143
145
/// <param name="obj"></param>
144
146
/// <returns></returns>
145
- public override bool Equals ( object obj )
147
+ public override bool Equals ( object ? obj )
146
148
{
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 ) ;
152
150
}
153
151
154
152
/// <summary>
0 commit comments