@@ -3,6 +3,7 @@ namespace ReflectionAnalyzers
3
3
using System . Linq ;
4
4
using Gu . Roslyn . AnalyzerExtensions ;
5
5
using Microsoft . CodeAnalysis ;
6
+ using Microsoft . CodeAnalysis . CSharp ;
6
7
using Microsoft . CodeAnalysis . CSharp . Syntax ;
7
8
using Microsoft . CodeAnalysis . Diagnostics ;
8
9
@@ -41,7 +42,7 @@ internal static class GetX
41
42
if ( invocation . ArgumentList != null &&
42
43
invocation . TryGetTarget ( KnownSymbol . Type . GetMethod , context . SemanticModel , context . CancellationToken , out var getX ) &&
43
44
TryGetTargetType ( invocation , context , out targetType , out _ ) &&
44
- IsKnownSignature ( getX ) &&
45
+ IsKnownSignature ( getX , invocation ) &&
45
46
TryGetName ( invocation , getX , context , out nameArg , out targetName ) &&
46
47
( TryGetFlags ( invocation , getX , context , out flagsArg , out flags ) ||
47
48
TryGetDefaultFlags ( KnownSymbol . Type . GetMethod , out flags ) ) )
@@ -50,16 +51,6 @@ internal static class GetX
50
51
}
51
52
52
53
return null ;
53
-
54
- bool IsKnownSignature ( IMethodSymbol candidate )
55
- {
56
- // I don't know how binder works so limiting checks to what I know.
57
- return ( candidate . Parameters . TrySingle ( out var nameParameter ) &&
58
- nameParameter . Type == KnownSymbol . String ) ||
59
- ( candidate . Parameters . Length == 2 &&
60
- candidate . Parameters . TrySingle ( x => x . Type == KnownSymbol . String , out nameParameter ) &&
61
- candidate . Parameters . TrySingle ( x => x . Type == KnownSymbol . BindingFlags , out _ ) ) ;
62
- }
63
54
}
64
55
65
56
/// <summary>
@@ -76,7 +67,7 @@ bool IsKnownSignature(IMethodSymbol candidate)
76
67
if ( invocation . ArgumentList != null &&
77
68
invocation . TryGetTarget ( KnownSymbol . Type . GetMember , context . SemanticModel , context . CancellationToken , out var getX ) &&
78
69
TryGetTargetType ( invocation , context , out targetType , out _ ) &&
79
- IsKnownSignature ( getX ) &&
70
+ IsKnownSignature ( getX , invocation ) &&
80
71
TryGetName ( invocation , getX , context , out nameArg , out targetName ) &&
81
72
( TryGetFlags ( invocation , getX , context , out flagsArg , out flags ) ||
82
73
TryGetDefaultFlags ( KnownSymbol . Type . GetMember , out flags ) ) )
@@ -85,16 +76,6 @@ bool IsKnownSignature(IMethodSymbol candidate)
85
76
}
86
77
87
78
return null ;
88
-
89
- bool IsKnownSignature ( IMethodSymbol candidate )
90
- {
91
- // I don't know how binder works so limiting checks to what I know.
92
- return ( candidate . Parameters . TrySingle ( out var nameParameter ) &&
93
- nameParameter . Type == KnownSymbol . String ) ||
94
- ( candidate . Parameters . Length == 2 &&
95
- candidate . Parameters . TrySingle ( x => x . Type == KnownSymbol . String , out nameParameter ) &&
96
- candidate . Parameters . TrySingle ( x => x . Type == KnownSymbol . BindingFlags , out _ ) ) ;
97
- }
98
79
}
99
80
100
81
/// <summary>
@@ -328,7 +309,7 @@ bool IsOverriding(ISymbol symbol, ISymbol candidateBase)
328
309
return false ;
329
310
}
330
311
331
- bool IsExplicitImplementation ( out ISymbol result )
312
+ bool IsExplicitImplementation ( out ISymbol result )
332
313
{
333
314
foreach ( var @interface in targetType . AllInterfaces )
334
315
{
@@ -378,6 +359,33 @@ internal static bool TryGetDefaultFlags(QualifiedMethod getX, out BindingFlags d
378
359
return false ;
379
360
}
380
361
362
+ /// <summary>
363
+ /// Defensive check to only handle known cases. Don't know how the binder works.
364
+ /// </summary>
365
+ private static bool IsKnownSignature ( IMethodSymbol candidate , InvocationExpressionSyntax invocation )
366
+ {
367
+ foreach ( var parameter in candidate . Parameters )
368
+ {
369
+ if ( ! IsKnownArgument ( parameter ) )
370
+ {
371
+ return false ;
372
+ }
373
+ }
374
+
375
+ return true ;
376
+ bool IsKnownArgument ( IParameterSymbol parameter )
377
+ {
378
+ if ( parameter . Type == KnownSymbol . String ||
379
+ parameter . Type == KnownSymbol . BindingFlags )
380
+ {
381
+ return true ;
382
+ }
383
+
384
+ return invocation . TryFindArgument ( parameter , out var argument ) &&
385
+ argument . Expression ? . IsKind ( SyntaxKind . NullLiteralExpression ) == true ;
386
+ }
387
+ }
388
+
381
389
/// <summary>
382
390
/// Handles GetField, GetEvent, GetMember, GetMethod...
383
391
/// </summary>
0 commit comments