PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/ILSpy/TreeNodes/Analyzer/AnalyzedInterfacePropertyImplementedByTreeNode.cs

http://github.com/icsharpcode/ILSpy
C# | 86 lines | 59 code | 10 blank | 17 comment | 11 complexity | 01e4c523329d3ce8a1946ea52dba7830 MD5 | raw file
 1// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
 2// 
 3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 4// software and associated documentation files (the "Software"), to deal in the Software
 5// without restriction, including without limitation the rights to use, copy, modify, merge,
 6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 7// to whom the Software is furnished to do so, subject to the following conditions:
 8// 
 9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11// 
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Threading;
23using ICSharpCode.Decompiler.Ast;
24using Mono.Cecil;
25
26namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
27{
28	internal sealed class AnalyzedInterfacePropertyImplementedByTreeNode : AnalyzerSearchTreeNode
29	{
30		private readonly PropertyDefinition analyzedProperty;
31		private readonly MethodDefinition analyzedMethod;
32
33		public AnalyzedInterfacePropertyImplementedByTreeNode(PropertyDefinition analyzedProperty)
34		{
35			if (analyzedProperty == null)
36				throw new ArgumentNullException("analyzedProperty");
37
38			this.analyzedProperty = analyzedProperty;
39			this.analyzedMethod = this.analyzedProperty.GetMethod ?? this.analyzedProperty.SetMethod;
40		}
41
42		public override object Text
43		{
44			get { return "Implemented By"; }
45		}
46
47		protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
48		{
49			var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedMethod, FindReferencesInType);
50			return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
51		}
52
53		private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
54		{
55			if (!type.HasInterfaces)
56				yield break;
57			TypeReference implementedInterfaceRef = type.Interfaces.FirstOrDefault(i => i.Resolve() == analyzedMethod.DeclaringType);
58			if (implementedInterfaceRef == null)
59				yield break;
60
61			foreach (PropertyDefinition property in type.Properties.Where(e => e.Name == analyzedProperty.Name)) {
62				MethodDefinition accessor = property.GetMethod ?? property.SetMethod;
63				if (TypesHierarchyHelpers.MatchInterfaceMethod(accessor, analyzedMethod, implementedInterfaceRef)) {
64					var node = new AnalyzedPropertyTreeNode(property);
65					node.Language = this.Language;
66					yield return node;
67				}
68				yield break;
69			}
70
71			foreach (PropertyDefinition property in type.Properties.Where(e => e.Name.EndsWith(analyzedProperty.Name))) {
72				MethodDefinition accessor = property.GetMethod ?? property.SetMethod;
73				if (accessor.HasOverrides && accessor.Overrides.Any(m => m.Resolve() == analyzedMethod)) {
74					var node = new AnalyzedPropertyTreeNode(property);
75					node.Language = this.Language;
76					yield return node;
77				}
78			}
79		}
80
81		public static bool CanShow(PropertyDefinition property)
82		{
83			return property.DeclaringType.IsInterface;
84		}
85	}
86}