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