Skip to content

Commit 374e0b5

Browse files
committed
new object []{} -> Array.Empty<object>()
1 parent e69da15 commit 374e0b5

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

WebApiClientCore.Analyzers.SourceGenerator/HttpApiCodeBuilder.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ public HttpApiCodeBuilder(INamedTypeSymbol httpApi)
7474
this.httpApi = httpApi;
7575
}
7676

77+
/// <summary>
78+
/// 获取所有方法的返回类型的Result类型
79+
/// </summary>
80+
/// <returns></returns>
81+
public IEnumerable<ITypeSymbol> GetResultTypes()
82+
{
83+
var methods = HttpApiMethodFinder.FindApiMethods(this.httpApi);
84+
foreach (var method in methods)
85+
{
86+
if (method.ReturnType is INamedTypeSymbol typeSymbol)
87+
{
88+
var resultType = typeSymbol.TypeArguments.FirstOrDefault();
89+
if (resultType != null)
90+
{
91+
yield return resultType;
92+
}
93+
}
94+
}
95+
}
96+
7797
/// <summary>
7898
/// 转换为SourceText
7999
/// </summary>
@@ -128,22 +148,6 @@ public override string ToString()
128148
}
129149

130150

131-
public IEnumerable<ITypeSymbol> GetResultTypes()
132-
{
133-
var methods = HttpApiMethodFinder.FindApiMethods(this.httpApi);
134-
foreach (var method in methods)
135-
{
136-
if (method.ReturnType is INamedTypeSymbol typeSymbol)
137-
{
138-
var resultType = typeSymbol.TypeArguments.FirstOrDefault();
139-
if (resultType != null)
140-
{
141-
yield return resultType;
142-
}
143-
}
144-
}
145-
}
146-
147151
/// <summary>
148152
/// 构建方法
149153
/// </summary>
@@ -155,11 +159,14 @@ private string BuildMethod(IMethodSymbol method, int index)
155159
var builder = new StringBuilder();
156160
var parametersString = string.Join(",", method.Parameters.Select(item => $"{item.Type} {item.Name}"));
157161
var parameterNamesString = string.Join(",", method.Parameters.Select(item => item.Name));
162+
var paremterArrayString = string.IsNullOrEmpty(parameterNamesString)
163+
? "Array.Empty<object>()"
164+
: $"new object[] {{ {parameterNamesString} }}";
158165

159166
builder.AppendLine($"\t\t[HttpApiProxyMethod({index})]");
160167
builder.AppendLine($"\t\tpublic {method.ReturnType} {method.Name}( {parametersString} )");
161168
builder.AppendLine("\t\t{");
162-
builder.AppendLine($"\t\t\treturn ({method.ReturnType})this.{this.apiInterceptorFieldName}.Intercept(this.{this.actionInvokersFieldName}[{index}], new object[] {{ {parameterNamesString} }});");
169+
builder.AppendLine($"\t\t\treturn ({method.ReturnType})this.{this.apiInterceptorFieldName}.Intercept(this.{this.actionInvokersFieldName}[{index}], {paremterArrayString});");
163170
builder.AppendLine("\t\t}");
164171
return builder.ToString();
165172
}

0 commit comments

Comments
 (0)