Skip to content

Commit

Permalink
BUGFIX REFL014 handle nested types when inheritance.
Browse files Browse the repository at this point in the history
Fix #229
  • Loading branch information
JohanLarsson committed May 16, 2020
1 parent ab2f038 commit 31465b3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ReflectionAnalyzers.Tests.REFL014PreferGetMemberThenAccessorTests
namespace ReflectionAnalyzers.Tests.REFL014PreferGetMemberThenAccessorTests
{
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -642,5 +642,86 @@ event EventHandler IC1.E
var message = @"Prefer typeof(BinaryReferencedAssembly.C1).GetEvent(""E"", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).AddMethod.";
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic.WithMessage(message), solution, after);
}

[Test]
public static void InNestedType()
{
var before = @"
namespace N
{
using System.Reflection;
class C
{
protected bool P { get; }
class Nested
{
object Get => typeof(C).↓GetMethod(""get_P"", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
}
}
}";

var after = @"
namespace N
{
using System.Reflection;
class C
{
protected bool P { get; }
class Nested
{
object Get => typeof(C).GetProperty(nameof(C.P), BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).GetMethod;
}
}
}";

RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after);
}

[Test]
public static void InNestedTypeWhenInheritance()
{
var @base = @"
namespace N
{
class Base
{
protected bool P { get; }
}
}";

var before = @"
namespace N
{
using System.Reflection;
class C : Base
{
class Nested
{
object Get => typeof(C).↓GetMethod(""get_P"", BindingFlags.NonPublic | BindingFlags.Instance);
}
}
}";

var after = @"
namespace N
{
using System.Reflection;
class C : Base
{
class Nested
{
object Get => typeof(C).GetProperty(nameof(C.P), BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).GetMethod;
}
}
}";

RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, new[] { @base, before }, after);
}
}
}
4 changes: 2 additions & 2 deletions ReflectionAnalyzers/NodeAnalzers/GetXAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ private static bool UsesNameOfWrongMember(ReflectedMember member, Name name, Syn

private static bool IsPreferGetMemberThenAccessor(ReflectedMember member, Name name, Flags flags, Types types, SyntaxNodeAnalysisContext context, [NotNullWhen(true)] out string? callText)
{
if (member.Invocation?.Expression is MemberAccessExpressionSyntax memberAccess)
if (member is { ReflectedType: { }, Invocation: { Expression: MemberAccessExpressionSyntax memberAccess } })
{
if (member is { Match: FilterMatch.Single, Symbol: IMethodSymbol method })
{
Expand Down Expand Up @@ -634,7 +634,7 @@ string MemberName(ISymbol associatedSymbol)
}

return context.SemanticModel.IsAccessible(context.Node.SpanStart, associatedSymbol)
? $"nameof({associatedSymbol.ContainingType.ToString(context)}.{associatedSymbol.Name})"
? $"nameof({member.ReflectedType.ToString(context)}.{associatedSymbol.Name})"
: $"\"{associatedSymbol.Name}\"";
}
}
Expand Down

0 comments on commit 31465b3

Please sign in to comment.