PageRenderTime 69ms CodeModel.GetById 65ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/ILSpy/TreeNodes/Analyzer/AnalyzedEventOverridesTreeNode.cs

http://github.com/icsharpcode/ILSpy
C# | 73 lines | 47 code | 9 blank | 17 comment | 6 complexity | 81b0abf2d4439f571bb2545ccaa7b30a 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 AnalyzedEventOverridesTreeNode : AnalyzerSearchTreeNode
29	{
30		private readonly EventDefinition analyzedEvent;
31
32		public AnalyzedEventOverridesTreeNode(EventDefinition analyzedEvent)
33		{
34			if (analyzedEvent == null)
35				throw new ArgumentNullException("analyzedEvent");
36
37			this.analyzedEvent = analyzedEvent;
38		}
39
40		public override object Text
41		{
42			get { return "Overridden By"; }
43		}
44
45		protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
46		{
47			var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedEvent, FindReferencesInType);
48			return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
49		}
50
51		private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
52		{
53			if (!TypesHierarchyHelpers.IsBaseType(analyzedEvent.DeclaringType, type, resolveTypeArguments: false))
54				yield break;
55
56			foreach (EventDefinition eventDef in type.Events) {
57				if (TypesHierarchyHelpers.IsBaseEvent(analyzedEvent, eventDef)) {
58					MethodDefinition anyAccessor = eventDef.AddMethod ?? eventDef.RemoveMethod;
59					bool hidesParent = !anyAccessor.IsVirtual ^ anyAccessor.IsNewSlot;
60					var node = new AnalyzedEventTreeNode(eventDef, hidesParent ? "(hides) " : "");
61					node.Language = this.Language;
62					yield return node;
63				}
64			}
65		}
66
67		public static bool CanShow(EventDefinition property)
68		{
69			var accessor = property.AddMethod ?? property.RemoveMethod;
70			return accessor.IsVirtual && !accessor.IsFinal && !accessor.DeclaringType.IsInterface;
71		}
72	}
73}