You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.6 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2. // This code is distributed under MIT X11 license (for details please see \doc\license.txt)
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using ICSharpCode.TreeView;
  11. using Mono.Cecil;
  12. namespace ICSharpCode.ILSpy
  13. {
  14. sealed class AssemblyTreeNode : SharpTreeNode
  15. {
  16. readonly string fileName;
  17. readonly string name;
  18. readonly Task<AssemblyDefinition> assemblyTask;
  19. readonly List<TypeTreeNode> classes = new List<TypeTreeNode>();
  20. readonly Dictionary<string, NamespaceTreeNode> namespaces = new Dictionary<string, NamespaceTreeNode>();
  21. public AssemblyTreeNode(string fileName)
  22. {
  23. if (fileName == null)
  24. throw new ArgumentNullException("fileName");
  25. this.fileName = fileName;
  26. this.assemblyTask = Task.Factory.StartNew<AssemblyDefinition>(LoadAssembly); // requires that this.fileName is set
  27. this.name = Path.GetFileNameWithoutExtension(fileName);
  28. this.LazyLoading = true;
  29. }
  30. public string FileName {
  31. get { return fileName; }
  32. }
  33. public override object Text {
  34. get { return name; }
  35. }
  36. public override object Icon {
  37. get { return Images.Assembly; }
  38. }
  39. AssemblyDefinition LoadAssembly()
  40. {
  41. // runs on background thread
  42. AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(fileName);
  43. foreach (TypeDefinition type in assembly.MainModule.Types.OrderBy(t => t.FullName)) {
  44. classes.Add(new TypeTreeNode(type));
  45. }
  46. return assembly;
  47. }
  48. protected override void LoadChildren()
  49. {
  50. assemblyTask.Wait();
  51. this.Children.Add(new ReferenceFolderTreeNode(assemblyTask.Result.MainModule));
  52. foreach (NamespaceTreeNode ns in namespaces.Values) {
  53. ns.Children.Clear();
  54. }
  55. foreach (TypeTreeNode type in classes) {
  56. if (showInternalAPI == false && type.IsPublicAPI == false)
  57. continue;
  58. NamespaceTreeNode ns;
  59. if (!namespaces.TryGetValue(type.Namespace, out ns)) {
  60. ns = new NamespaceTreeNode(type.Namespace);
  61. namespaces[type.Namespace] = ns;
  62. }
  63. ns.Children.Add(type);
  64. }
  65. foreach (NamespaceTreeNode ns in namespaces.Values.OrderBy(n => n.Name)) {
  66. if (ns.Children.Count > 0)
  67. this.Children.Add(ns);
  68. }
  69. }
  70. /// <summary>
  71. /// Invalidates the list of children.
  72. /// </summary>
  73. void InvalidateChildren()
  74. {
  75. this.Children.Clear();
  76. if (this.IsExpanded)
  77. this.LoadChildren();
  78. else
  79. this.LazyLoading = true;
  80. }
  81. bool showInternalAPI = true;
  82. public bool ShowInternalAPI {
  83. get { return showInternalAPI; }
  84. set {
  85. if (showInternalAPI != value) {
  86. showInternalAPI = value;
  87. InvalidateChildren();
  88. RaisePropertyChanged("ShowInternalAPI");
  89. }
  90. }
  91. }
  92. public override bool CanDrag(SharpTreeNode[] nodes)
  93. {
  94. return nodes.All(n => n is AssemblyTreeNode);
  95. }
  96. public override bool CanDelete(SharpTreeNode[] nodes)
  97. {
  98. return Parent != null && Parent.CanDelete(nodes); // handle deletion in the AssemblyListTreeNode
  99. }
  100. public override void Delete(SharpTreeNode[] nodes)
  101. {
  102. Parent.Delete(nodes); // handle deletion in the AssemblyListTreeNode
  103. }
  104. public override void DeleteCore(SharpTreeNode[] nodes)
  105. {
  106. Parent.DeleteCore(nodes); // handle deletion in the AssemblyListTreeNode
  107. }
  108. public override IDataObject Copy(SharpTreeNode[] nodes)
  109. {
  110. DataObject dataObject = new DataObject();
  111. dataObject.SetData("ILSpyAssemblies", nodes.OfType<AssemblyTreeNode>());
  112. return dataObject;
  113. }
  114. }
  115. }