PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/ILSpy/TreeNodes/Analyzer/AnalyzedTypeExtensionMethodsTreeNode.cs

http://github.com/icsharpcode/ILSpy
C# | 84 lines | 56 code | 10 blank | 18 comment | 14 complexity | a4a62b9be657a88f498c2ef045c90a8e 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 Mono.Cecil;
24
25namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
26{
27	internal class AnalyzedTypeExtensionMethodsTreeNode : AnalyzerSearchTreeNode
28	{
29		private readonly TypeDefinition analyzedType;
30
31		public AnalyzedTypeExtensionMethodsTreeNode(TypeDefinition analyzedType)
32		{
33			if (analyzedType == null)
34				throw new ArgumentNullException("analyzedType");
35
36			this.analyzedType = analyzedType;
37		}
38
39		public override object Text
40		{
41			get { return "Extension Methods"; }
42		}
43
44		protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
45		{
46			var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedType, FindReferencesInType);
47			return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
48		}
49
50		private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
51		{
52			if (!HasExtensionAttribute(type))
53				yield break;
54			foreach (MethodDefinition method in type.Methods) {
55				if (method.IsStatic && HasExtensionAttribute(method)) {
56					if (method.HasParameters && method.Parameters[0].ParameterType.Resolve() == analyzedType) {
57						var node = new AnalyzedMethodTreeNode(method);
58						node.Language = this.Language;
59						yield return node;
60					}
61				}
62			}
63		}
64		
65		bool HasExtensionAttribute(ICustomAttributeProvider p)
66		{
67			if (p.HasCustomAttributes) {
68				foreach (CustomAttribute ca in p.CustomAttributes) {
69					TypeReference t = ca.AttributeType;
70					if (t.Name == "ExtensionAttribute" && t.Namespace == "System.Runtime.CompilerServices")
71						return true;
72				}
73			}
74			return false;
75		}
76		
77
78		public static bool CanShow(TypeDefinition type)
79		{
80			// show on all types except static classes
81			return !(type.IsAbstract && type.IsSealed);
82		}
83	}
84}