Skip to content

Commit 392f866

Browse files
committed
Simplify use IsAssignableTo
1 parent b1916cd commit 392f866

3 files changed

Lines changed: 22 additions & 24 deletions

File tree

ReflectionAnalyzers.Tests/Helpers/Filters/TypesTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public void M(Type2 _) { }
3838
var m2 = semanticModel.GetDeclaredSymbol(syntaxTree.FindMethodDeclaration(signature2), CancellationToken.None);
3939
var invocation = syntaxTree.FindInvocation("GetMethod");
4040
Assert.AreEqual(true, Types.TryCreate(invocation, (IMethodSymbol?)semanticModel.GetSymbolInfo(invocation).Symbol, semanticModel, CancellationToken.None, out var types));
41-
Assert.AreEqual(true, types.TryMostSpecific(m1, m2, out var match));
41+
Assert.AreEqual(true, types.TryMostSpecific(m1, m2, compilation, out var match));
4242
Assert.AreEqual(m1, match);
43-
Assert.AreEqual(true, types.TryMostSpecific(m2, m1, out match));
43+
Assert.AreEqual(true, types.TryMostSpecific(m2, m1, compilation, out match));
4444
Assert.AreEqual(m1, match);
4545
}
4646

@@ -71,7 +71,7 @@ public void M(Type2 _) { }
7171
var m2 = semanticModel.GetDeclaredSymbol(syntaxTree.FindMethodDeclaration(signature2), CancellationToken.None);
7272
var invocation = syntaxTree.FindInvocation("GetMethod");
7373
Assert.AreEqual(true, Types.TryCreate(invocation, (IMethodSymbol?)semanticModel.GetSymbolInfo(invocation).Symbol, semanticModel, CancellationToken.None, out var types));
74-
Assert.AreEqual(false, types.TryMostSpecific(m1, m2, out _));
74+
Assert.AreEqual(false, types.TryMostSpecific(m1, m2, compilation, out _));
7575
}
7676
}
7777
}

ReflectionAnalyzers/Helpers/Reflection/Filters/Types.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ internal static bool TryGetTypesArrayText(ImmutableArray<IParameterSymbol> param
7575
return true;
7676
}
7777

78-
internal bool Matches(ImmutableArray<IParameterSymbol> parameters)
78+
internal bool Matches(ImmutableArray<IParameterSymbol> parameters, Compilation compilation)
7979
{
8080
if (parameters.Length != this.Expressions.Length)
8181
{
@@ -84,9 +84,7 @@ internal bool Matches(ImmutableArray<IParameterSymbol> parameters)
8484

8585
for (var i = 0; i < parameters.Length; i++)
8686
{
87-
#pragma warning disable CS0618 // Type or member is obsolete
88-
if (!this.Symbols[i].Is(parameters[i].Type))
89-
#pragma warning restore CS0618 // Type or member is obsolete
87+
if (!this.Symbols[i].IsAssignableTo(parameters[i].Type, compilation))
9088
{
9189
return false;
9290
}
@@ -95,7 +93,7 @@ internal bool Matches(ImmutableArray<IParameterSymbol> parameters)
9593
return true;
9694
}
9795

98-
internal bool TryMostSpecific(ISymbol? x, ISymbol? y, [NotNullWhen(true)] out ISymbol? unique)
96+
internal bool TryMostSpecific(ISymbol? x, ISymbol? y, Compilation compilation, [NotNullWhen(true)] out ISymbol? unique)
9997
{
10098
if (x is null &&
10199
y is null)
@@ -118,7 +116,7 @@ internal bool TryMostSpecific(ISymbol? x, ISymbol? y, [NotNullWhen(true)] out IS
118116
return false;
119117
}
120118

121-
return this.TryMostSpecific(x as IMethodSymbol, y as IMethodSymbol, out unique);
119+
return this.TryMostSpecific(x as IMethodSymbol, y as IMethodSymbol, compilation, out unique);
122120

123121
static bool ByNull(ISymbol? first, ISymbol? other, out ISymbol? result)
124122
{
@@ -141,7 +139,7 @@ private static bool TryGetTypesArgument(InvocationExpressionSyntax invocation, I
141139
invocation.TryFindArgument(parameter, out argument);
142140
}
143141

144-
private bool TryMostSpecific(IMethodSymbol? x, IMethodSymbol? y, [NotNullWhen(true)] out ISymbol? unique)
142+
private bool TryMostSpecific(IMethodSymbol? x, IMethodSymbol? y, Compilation compilation, [NotNullWhen(true)] out ISymbol? unique)
145143
{
146144
if (this.Argument is null ||
147145
x is null ||
@@ -159,8 +157,8 @@ x is null ||
159157
#pragma warning restore CS8762 // Parameter must have a non-null value when exiting in some condition.
160158
}
161159

162-
if (this.Matches(x.Parameters) &&
163-
this.Matches(y.Parameters))
160+
if (this.Matches(x.Parameters, compilation) &&
161+
this.Matches(y.Parameters, compilation))
164162
{
165163
var sum = 0;
166164
for (var i = 0; i < this.Symbols.Length; i++)
@@ -178,13 +176,13 @@ x is null ||
178176
return true;
179177
}
180178

181-
if (this.Matches(x.Parameters))
179+
if (this.Matches(x.Parameters, compilation))
182180
{
183181
unique = x;
184182
return true;
185183
}
186184

187-
if (this.Matches(y.Parameters))
185+
if (this.Matches(y.Parameters, compilation))
188186
{
189187
unique = y;
190188
return true;

ReflectionAnalyzers/Helpers/Reflection/ReflectedMember.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ private static FilterMatch TryGetMember(IMethodSymbol getX, ITypeSymbol type, Na
100100
{
101101
foreach (var candidate in type.GetMembers())
102102
{
103-
if (!MatchesFilter(candidate, name, flags, types))
103+
if (!MatchesFilter(candidate, compilation, name, flags, types))
104104
{
105105
continue;
106106
}
107107

108-
if (types.TryMostSpecific(member, candidate, out member))
108+
if (types.TryMostSpecific(member, candidate, compilation, out member))
109109
{
110110
isAmbiguous = false;
111111
if (IsWrongMemberType(member))
@@ -126,7 +126,7 @@ private static FilterMatch TryGetMember(IMethodSymbol getX, ITypeSymbol type, Na
126126
{
127127
foreach (var candidate in current.GetMembers())
128128
{
129-
if (!MatchesFilter(candidate, name, flags, types))
129+
if (!MatchesFilter(candidate, compilation, name, flags, types))
130130
{
131131
continue;
132132
}
@@ -136,7 +136,7 @@ private static FilterMatch TryGetMember(IMethodSymbol getX, ITypeSymbol type, Na
136136
continue;
137137
}
138138

139-
if (types.TryMostSpecific(member, candidate, out member))
139+
if (types.TryMostSpecific(member, candidate, compilation, out member))
140140
{
141141
isAmbiguous = false;
142142
if (IsUseContainingType(member))
@@ -182,7 +182,7 @@ private static FilterMatch TryGetMember(IMethodSymbol getX, ITypeSymbol type, Na
182182
return FilterMatch.Single;
183183
}
184184

185-
if (type.TryFindFirstMemberRecursive(x => MatchesFilter(x, name, Flags.MatchAll.Effective, Types.Any), out member))
185+
if (type.TryFindFirstMemberRecursive(x => MatchesFilter(x, compilation, name, Flags.MatchAll.Effective, Types.Any), out member))
186186
{
187187
if (IsUseContainingType(member))
188188
{
@@ -294,7 +294,7 @@ bool IsUseContainingType(ISymbol symbol)
294294
bool IsWrongFlags(ISymbol symbol)
295295
{
296296
if (symbol.MetadataName == name.MetadataName &&
297-
!MatchesFilter(symbol, name, flags, Types.Any))
297+
!MatchesFilter(symbol, compilation, name, flags, Types.Any))
298298
{
299299
return true;
300300
}
@@ -318,14 +318,14 @@ bool IsWrongTypes(ISymbol symbol)
318318

319319
const BindingFlags everything = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
320320
return symbol.MetadataName == name.MetadataName &&
321-
!MatchesFilter(symbol, name, everything, types);
321+
!MatchesFilter(symbol, compilation, name, everything, types);
322322
}
323323

324324
bool IsExplicitImplementation(out ISymbol result)
325325
{
326326
foreach (var @interface in type.AllInterfaces)
327327
{
328-
if (@interface.TryFindFirstMember(x => MatchesFilter(x, name, Flags.MatchAll.Effective, types), out result!))
328+
if (@interface.TryFindFirstMember(x => MatchesFilter(x, compilation, name, Flags.MatchAll.Effective, types), out result!))
329329
{
330330
return true;
331331
}
@@ -336,7 +336,7 @@ bool IsExplicitImplementation(out ISymbol result)
336336
}
337337
}
338338

339-
private static bool MatchesFilter(ISymbol candidate, Name name, BindingFlags flags, Types types)
339+
private static bool MatchesFilter(ISymbol candidate, Compilation compilation, Name name, BindingFlags flags, Types types)
340340
{
341341
if (candidate.MetadataName != name.MetadataName)
342342
{
@@ -371,7 +371,7 @@ when IsMember() &&
371371
{
372372
switch (candidate)
373373
{
374-
case IMethodSymbol method when !types.Matches(method.Parameters):
374+
case IMethodSymbol method when !types.Matches(method.Parameters, compilation):
375375
return false;
376376
}
377377
}

0 commit comments

Comments
 (0)