2
// VariableHidesMemberIssue.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
using System.Collections.Generic;
28
using ICSharpCode.NRefactory.Semantics;
29
using ICSharpCode.NRefactory.TypeSystem;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
34
public abstract class VariableHidesMemberIssue : ICodeIssueProvider
36
public abstract IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context);
38
protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
40
var typeDecl = node.GetParent<TypeDeclaration>();
43
var entityDecl = node.GetParent<EntityDeclaration>();
44
var memberResolveResult = ctx.Resolve(entityDecl) as MemberResolveResult;
45
if (memberResolveResult == null)
47
var typeResolveResult = ctx.Resolve(typeDecl) as TypeResolveResult;
48
if (typeResolveResult == null)
51
var sourceMember = memberResolveResult.Member;
53
return typeResolveResult.Type.GetMembers(m => m.Name == variableName).Any(m2 => IsAccessible(sourceMember, m2));
56
static bool IsAccessible(IMember sourceMember, IMember targetMember)
58
if (sourceMember.IsStatic != targetMember.IsStatic)
61
var sourceType = sourceMember.DeclaringType;
62
var targetType = targetMember.DeclaringType;
63
switch (targetMember.Accessibility) {
64
case Accessibility.None:
66
case Accessibility.Private:
67
// check for members of outer classes (private members of outer classes can be accessed)
68
var targetTypeDefinition = targetType.GetDefinition();
69
for (var t = sourceType.GetDefinition(); t != null; t = t.DeclaringTypeDefinition) {
70
if (t.Equals(targetTypeDefinition))
74
case Accessibility.Public:
76
case Accessibility.Protected:
77
return IsProtectedAccessible(sourceType, targetType);
78
case Accessibility.Internal:
79
return IsInternalAccessible(sourceMember.ParentAssembly, targetMember.ParentAssembly);
80
case Accessibility.ProtectedOrInternal:
81
return IsInternalAccessible(sourceMember.ParentAssembly, targetMember.ParentAssembly) || IsProtectedAccessible(sourceType, targetType);
82
case Accessibility.ProtectedAndInternal:
83
return IsInternalAccessible(sourceMember.ParentAssembly, targetMember.ParentAssembly) && IsProtectedAccessible(sourceType, targetType);
85
throw new Exception("Invalid value for Accessibility");
89
static bool IsProtectedAccessible(IType sourceType, IType targetType)
91
return sourceType.GetAllBaseTypes().Any(type => targetType.Equals(type));
94
static bool IsInternalAccessible(IAssembly sourceAssembly, IAssembly targetAssembly)
96
return sourceAssembly.InternalsVisibleTo(targetAssembly);