Browse Source

Fixed accessibility check for protected members in outer classes.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
1467ce3b5b
  1. 10
      ICSharpCode.NRefactory.CSharp/Resolver/MemberLookup.cs
  2. 16
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/MemberLookupTests.cs

10
ICSharpCode.NRefactory.CSharp/Resolver/MemberLookup.cs

@ -66,7 +66,13 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
public bool IsProtectedAccessAllowed(IType targetType) public bool IsProtectedAccessAllowed(IType targetType)
{ {
ITypeDefinition typeDef = targetType.GetDefinition(); ITypeDefinition typeDef = targetType.GetDefinition();
return typeDef != null && typeDef.IsDerivedFrom(currentTypeDefinition); if (typeDef == null)
return false;
for (ITypeDefinition c = currentTypeDefinition; c != null; c = c.DeclaringTypeDefinition) {
if (typeDef.IsDerivedFrom(c))
return true;
}
return false;
} }
/// <summary> /// <summary>
@ -126,7 +132,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
// PERF: this might hurt performance as this method is called several times (once for each member) // PERF: this might hurt performance as this method is called several times (once for each member)
// make sure resolving base types is cheap (caches?) or cache within the MemberLookup instance // make sure resolving base types is cheap (caches?) or cache within the MemberLookup instance
if (allowProtectedAccess && currentTypeDefinition.IsDerivedFrom(entity.DeclaringTypeDefinition)) if (allowProtectedAccess && t.IsDerivedFrom(entity.DeclaringTypeDefinition))
return true; return true;
} }
return false; return false;

16
ICSharpCode.NRefactory.Tests/CSharp/Resolver/MemberLookupTests.cs

@ -261,5 +261,21 @@ public struct C<T> {
Assert.AreEqual("C`1[[`0]]", rr.Arguments[0].Type.ReflectionName); Assert.AreEqual("C`1[[`0]]", rr.Arguments[0].Type.ReflectionName);
Assert.AreEqual("C`1[[`0]]", rr.Arguments[1].Type.ReflectionName); Assert.AreEqual("C`1[[`0]]", rr.Arguments[1].Type.ReflectionName);
} }
[Test]
public void ProtectedFieldInOuterClass()
{
string program = @"using System;
class Base {
protected int X;
}
class Derived : Base {
class Inner {
public int M(Derived d) { return $d.X$; }
}}";
var rr = Resolve<MemberResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("Base.X", rr.Member.FullName);
}
} }
} }

Loading…
Cancel
Save