Skip to content

Commit 4fae272

Browse files
committed
兼容v2.0.8和2.0.9的SourceGenerator语法
1 parent b965eef commit 4fae272

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

WebApiClientCore.Analyzers/SourceGenerator/HttpApiProxyClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private string BuildMethod(INamedTypeSymbol interfaceType, IMethodSymbol method,
138138
: $"new global::System.Object[] {{ {parameterNamesString} }}";
139139

140140
var returnTypeString = GetFullName(method.ReturnType);
141-
builder.AppendLine($"\t\t[global::WebApiClientCore.HttpApiProxyMethod({index}, typeof({GetFullName(interfaceType)}), \"{method.Name}\")]");
141+
builder.AppendLine($"\t\t[global::WebApiClientCore.HttpApiProxyMethod({index}, \"{method.Name}\", typeof({GetFullName(interfaceType)}))]");
142142
builder.AppendLine($"\t\t{returnTypeString} {GetFullName(interfaceType)}.{method.Name}( {parametersString} )");
143143
builder.AppendLine("\t\t{");
144144
builder.AppendLine($"\t\t\treturn ({returnTypeString})this.{this.apiInterceptorFieldName}.Intercept(this.{this.actionInvokersFieldName}[{index}], {paremterArrayString});");

WebApiClientCore/HttpApiProxyMethodAttribute.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,48 @@ public sealed class HttpApiProxyMethodAttribute : Attribute
1313
/// <summary>
1414
/// 获取索引值
1515
/// </summary>
16-
public int Index { get; }
16+
public int Index { get; } = -1;
17+
18+
/// <summary>
19+
/// 获取名称
20+
/// </summary>
21+
public string Name { get; } = string.Empty;
1722

1823
/// <summary>
1924
/// 获取方法所在的声明类型
2025
/// </summary>
2126
public Type? DeclaringType { get; }
2227

2328
/// <summary>
24-
/// 获取名称
29+
/// 方法的索引特性
2530
/// </summary>
26-
public string Name { get; }
31+
/// <param name="index">索引值</param>
32+
[Obsolete("仅为了兼容v2.0.8的SourceGenerator语法")]
33+
public HttpApiProxyMethodAttribute(int index)
34+
{
35+
}
2736

2837
/// <summary>
2938
/// 方法的索引特性
3039
/// </summary>
3140
/// <param name="index">索引值</param>
32-
/// <param name="declaringType">法所在的声明类型</param>
3341
/// <param name="name">方法的名称</param>
34-
public HttpApiProxyMethodAttribute(int index, Type? declaringType, string name)
42+
[Obsolete("仅为了兼容v2.0.9的SourceGenerator语法")]
43+
public HttpApiProxyMethodAttribute(int index, string name)
44+
{
45+
}
46+
47+
/// <summary>
48+
/// 方法的索引特性
49+
/// </summary>
50+
/// <param name="index">索引值</param>
51+
/// <param name="name">方法的名称</param>
52+
/// <param name="declaringType">法所在的声明类型</param>
53+
public HttpApiProxyMethodAttribute(int index, string name, Type? declaringType)
3554
{
3655
this.Index = index;
37-
this.DeclaringType = declaringType;
3856
this.Name = name;
57+
this.DeclaringType = declaringType;
3958
}
4059
}
4160
}

WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static IEnumerable<MethodInfo> FindApiMethods(Type httpApiType, Type pro
8383

8484
if (apiMethods.Length != classMethods.Length)
8585
{
86-
var message = $"接口类型{httpApiType}与其代理类不匹配,请重新编译接口类型所在的项目";
86+
var message = $"接口类型{httpApiType}的代理类{proxyClassType}和当前版本不兼容,请将{httpApiType.Assembly.GetName().Name}项目所依赖的WebApiClientCore更新到版本v{typeof(SourceGeneratorHttpApiActivator<>).Assembly.GetName().Version}";
8787
throw new ProxyTypeException(httpApiType, message);
8888
}
8989

0 commit comments

Comments
 (0)