PageRenderTime 31ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/ILSpy/TreeNodes/DerivedTypesTreeNode.cs

http://github.com/icsharpcode/ILSpy
C# | 100 lines | 68 code | 10 blank | 22 comment | 6 complexity | 064cc6eb7f6136bdf396bfb1b9645c7a 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.Collections.Generic;
 20using System.Linq;
 21using System.Threading;
 22using ICSharpCode.Decompiler;
 23using ICSharpCode.Decompiler.Metadata;
 24using ICSharpCode.Decompiler.TypeSystem;
 25using ICSharpCode.ILSpy.Properties;
 26using SRM = System.Reflection.Metadata;
 27
 28namespace ICSharpCode.ILSpy.TreeNodes
 29{
 30	/// <summary>
 31	/// Lists the sub types of a class.
 32	/// </summary>
 33	sealed class DerivedTypesTreeNode : ILSpyTreeNode
 34	{
 35		readonly AssemblyList list;
 36		readonly ITypeDefinition type;
 37		readonly ThreadingSupport threading;
 38
 39		public DerivedTypesTreeNode(AssemblyList list, ITypeDefinition type)
 40		{
 41			this.list = list;
 42			this.type = type;
 43			this.LazyLoading = true;
 44			this.threading = new ThreadingSupport();
 45		}
 46
 47		public override object Text => Resources.DerivedTypes;
 48
 49		public override object Icon => Images.SubTypes;
 50
 51		protected override void LoadChildren()
 52		{
 53			threading.LoadChildren(this, FetchChildren);
 54		}
 55
 56		IEnumerable<ILSpyTreeNode> FetchChildren(CancellationToken cancellationToken)
 57		{
 58			// FetchChildren() runs on the main thread; but the enumerator will be consumed on a background thread
 59			var assemblies = list.GetAssemblies().Select(node => node.GetPEFileOrNull()).Where(asm => asm != null).ToArray();
 60			return FindDerivedTypes(list, type, assemblies, cancellationToken);
 61		}
 62
 63		internal static IEnumerable<DerivedTypesEntryNode> FindDerivedTypes(AssemblyList list, ITypeDefinition type,
 64			PEFile[] assemblies, CancellationToken cancellationToken)
 65		{
 66			var definitionMetadata = type.ParentModule.PEFile.Metadata;
 67			var metadataToken = (SRM.TypeDefinitionHandle)type.MetadataToken;
 68			foreach (var module in assemblies) {
 69				var metadata = module.Metadata;
 70				var assembly = (MetadataModule)module.GetTypeSystemOrNull().MainModule;
 71				foreach (var h in metadata.TypeDefinitions) {
 72					cancellationToken.ThrowIfCancellationRequested();
 73					var td = metadata.GetTypeDefinition(h);
 74					foreach (var iface in td.GetInterfaceImplementations()) {
 75						var ifaceImpl = metadata.GetInterfaceImplementation(iface);
 76						if (!ifaceImpl.Interface.IsNil && IsSameType(metadata, ifaceImpl.Interface, definitionMetadata, metadataToken))
 77							yield return new DerivedTypesEntryNode(list, assembly.GetDefinition(h));
 78					}
 79					SRM.EntityHandle baseType = td.GetBaseTypeOrNil();
 80					if (!baseType.IsNil && IsSameType(metadata, baseType, definitionMetadata, metadataToken)) {
 81						yield return new DerivedTypesEntryNode(list, assembly.GetDefinition(h));
 82					}
 83				}
 84			}
 85			yield break;
 86		}
 87
 88		static bool IsSameType(SRM.MetadataReader referenceMetadata, SRM.EntityHandle typeRef,
 89			                   SRM.MetadataReader definitionMetadata, SRM.TypeDefinitionHandle typeDef)
 90		{
 91			// FullName contains only namespace, name and type parameter count, therefore this should suffice.
 92			return typeRef.GetFullTypeName(referenceMetadata) == typeDef.GetFullTypeName(definitionMetadata);
 93		}
 94
 95		public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
 96		{
 97			threading.Decompile(language, output, options, EnsureLazyChildren);
 98		}
 99	}
100}