Skip to content

Commit 0814982

Browse files
committed
SG支持接口声明new方法覆盖
1 parent c213c23 commit 0814982

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

WebApiClientCore.Analyzers.SourceGenerator/HttpApiCodeBuilder.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public override string ToString()
111111
builder.AppendLine("\t\t}");
112112

113113
var index = 0;
114-
foreach (var method in FindApiMethods(this.httpApi))
114+
foreach (var method in HttpApiMethodFinder.FindApiMethods(this.httpApi))
115115
{
116116
var methodCode = this.BuildMethod(method, index);
117117
builder.AppendLine(methodCode);
@@ -125,19 +125,6 @@ public override string ToString()
125125
return builder.ToString();
126126
}
127127

128-
/// <summary>
129-
/// 查找接口类型及其继承的接口的所有方法
130-
/// </summary>
131-
/// <param name="httpApi">接口</param>
132-
/// <returns></returns>
133-
private static IEnumerable<IMethodSymbol> FindApiMethods(INamedTypeSymbol httpApi)
134-
{
135-
return httpApi
136-
.AllInterfaces
137-
.Append(httpApi)
138-
.SelectMany(item => item.GetMembers())
139-
.OfType<IMethodSymbol>();
140-
}
141128

142129
/// <summary>
143130
/// 构建方法
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.CodeAnalysis;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace WebApiClientCore.Analyzers.SourceGenerator
7+
{
8+
/// <summary>
9+
/// HttpApi的方法查找器
10+
/// </summary>
11+
static class HttpApiMethodFinder
12+
{
13+
/// <summary>
14+
/// 查找接口类型及其继承的接口的所有方法
15+
/// </summary>
16+
/// <param name="httpApiType">接口类型</param>
17+
/// <returns></returns>
18+
public static IEnumerable<IMethodSymbol> FindApiMethods(INamedTypeSymbol httpApiType)
19+
{
20+
return httpApiType.AllInterfaces.Append(httpApiType)
21+
.SelectMany(item => item.GetMembers())
22+
.OfType<IMethodSymbol>()
23+
.Distinct(MethodEqualityComparer.Default);
24+
}
25+
26+
/// <summary>
27+
/// 表示MethodInfo的相等比较器
28+
/// </summary>
29+
private class MethodEqualityComparer : IEqualityComparer<IMethodSymbol>
30+
{
31+
public static MethodEqualityComparer Default { get; } = new MethodEqualityComparer();
32+
33+
public bool Equals(IMethodSymbol x, IMethodSymbol y)
34+
{
35+
if (x.Name != y.Name || !x.ReturnType.Equals(y.ReturnType, SymbolEqualityComparer.Default))
36+
{
37+
return false;
38+
}
39+
40+
var xParameterTypes = x.Parameters.Select(p => p.Type);
41+
var yParameterTypes = y.Parameters.Select(p => p.Type);
42+
return xParameterTypes.SequenceEqual(yParameterTypes, SymbolEqualityComparer.Default);
43+
}
44+
45+
public int GetHashCode(IMethodSymbol obj)
46+
{
47+
var hashCode = 0;
48+
hashCode ^= obj.Name.GetHashCode();
49+
hashCode ^= obj.ReturnType.GetHashCode();
50+
foreach (var parameter in obj.Parameters)
51+
{
52+
hashCode ^= parameter.Type.GetHashCode();
53+
}
54+
return hashCode;
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)