ClassExplorer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Terminal.Gui;
  8. namespace UICatalog.Scenarios {
  9. [ScenarioMetadata (Name: "Class Explorer", Description: "Tree view explorer for classes by namespace based on TreeView")]
  10. [ScenarioCategory ("Controls")]
  11. class ClassExplorer : Scenario {
  12. private TreeView<object> treeView;
  13. private TextView textView;
  14. private MenuItem miShowPrivate;
  15. private enum Showable {
  16. Properties,
  17. Fields,
  18. Events,
  19. Constructors,
  20. Methods,
  21. }
  22. private class ShowForType {
  23. public ShowForType (Showable toShow, Type type)
  24. {
  25. ToShow = toShow;
  26. Type = type;
  27. }
  28. public Type Type { get; set; }
  29. public Showable ToShow { get; set; }
  30. // Make sure to implement Equals methods on your objects if you intend to return new instances every time in ChildGetter
  31. public override bool Equals (object obj)
  32. {
  33. return obj is ShowForType type &&
  34. EqualityComparer<Type>.Default.Equals (Type, type.Type) &&
  35. ToShow == type.ToShow;
  36. }
  37. public override int GetHashCode ()
  38. {
  39. return HashCode.Combine (Type, ToShow);
  40. }
  41. public override string ToString ()
  42. {
  43. return ToShow.ToString ();
  44. }
  45. }
  46. public override void Setup ()
  47. {
  48. Win.Title = this.GetName ();
  49. Win.Y = 1; // menu
  50. Win.Height = Dim.Fill (1); // status bar
  51. Top.LayoutSubviews ();
  52. var menu = new MenuBar (new MenuBarItem [] {
  53. new MenuBarItem ("_File", new MenuItem [] {
  54. new MenuItem ("_Quit", "", () => Quit()),
  55. })
  56. ,
  57. new MenuBarItem ("_View", new MenuItem [] {
  58. miShowPrivate = new MenuItem ("_Include Private", "", () => ShowPrivate()){
  59. Checked = false,
  60. CheckType = MenuItemCheckStyle.Checked
  61. },
  62. new MenuItem ("_Expand All", "", () => treeView.ExpandAll()),
  63. new MenuItem ("_Collapse All", "", () => treeView.CollapseAll()) }),
  64. });
  65. Top.Add (menu);
  66. treeView = new TreeView<object> () {
  67. X = 0,
  68. Y = 0,
  69. Width = Dim.Percent (50),
  70. Height = Dim.Fill (),
  71. };
  72. treeView.AddObjects (AppDomain.CurrentDomain.GetAssemblies ());
  73. treeView.AspectGetter = GetRepresentation;
  74. treeView.TreeBuilder = new DelegateTreeBuilder<object> (ChildGetter, CanExpand);
  75. treeView.SelectionChanged += TreeView_SelectionChanged;
  76. Win.Add (treeView);
  77. textView = new TextView () {
  78. X = Pos.Right (treeView),
  79. Y = 0,
  80. Width = Dim.Fill (),
  81. Height = Dim.Fill ()
  82. };
  83. Win.Add (textView);
  84. }
  85. private void ShowPrivate ()
  86. {
  87. miShowPrivate.Checked = !miShowPrivate.Checked;
  88. treeView.RebuildTree ();
  89. treeView.SetFocus ();
  90. }
  91. private BindingFlags GetFlags ()
  92. {
  93. if (miShowPrivate.Checked) {
  94. return BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
  95. }
  96. return BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
  97. }
  98. private void TreeView_SelectionChanged (object sender, SelectionChangedEventArgs<object> e)
  99. {
  100. var val = e.NewValue;
  101. var all = treeView.GetAllSelectedObjects ().ToArray ();
  102. if (val == null || val is ShowForType) {
  103. return;
  104. }
  105. try {
  106. if (all.Length > 1) {
  107. textView.Text = all.Length + " Objects";
  108. } else {
  109. StringBuilder sb = new StringBuilder ();
  110. // tell the user about the currently selected tree node
  111. sb.AppendLine (e.NewValue.GetType ().Name);
  112. if (val is Assembly ass) {
  113. sb.AppendLine ($"Location:{ass.Location}");
  114. sb.AppendLine ($"FullName:{ass.FullName}");
  115. }
  116. if (val is PropertyInfo p) {
  117. sb.AppendLine ($"Name:{p.Name}");
  118. sb.AppendLine ($"Type:{p.PropertyType}");
  119. sb.AppendLine ($"CanWrite:{p.CanWrite}");
  120. sb.AppendLine ($"CanRead:{p.CanRead}");
  121. }
  122. if (val is FieldInfo f) {
  123. sb.AppendLine ($"Name:{f.Name}");
  124. sb.AppendLine ($"Type:{f.FieldType}");
  125. }
  126. if (val is EventInfo ev) {
  127. sb.AppendLine ($"Name:{ev.Name}");
  128. sb.AppendLine ($"Parameters:");
  129. foreach (var parameter in ev.EventHandlerType.GetMethod ("Invoke").GetParameters ()) {
  130. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  131. }
  132. }
  133. if (val is MethodInfo method) {
  134. sb.AppendLine ($"Name:{method.Name}");
  135. sb.AppendLine ($"IsPublic:{method.IsPublic}");
  136. sb.AppendLine ($"IsStatic:{method.IsStatic}");
  137. sb.AppendLine ($"Parameters:{(method.GetParameters ().Any () ? "" : "None")}");
  138. foreach (var parameter in method.GetParameters ()) {
  139. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  140. }
  141. }
  142. if (val is ConstructorInfo ctor) {
  143. sb.AppendLine ($"Name:{ctor.Name}");
  144. sb.AppendLine ($"Parameters:{(ctor.GetParameters ().Any () ? "" : "None")}");
  145. foreach (var parameter in ctor.GetParameters ()) {
  146. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  147. }
  148. }
  149. textView.Text = sb.ToString ().Replace ("\r\n", "\n");
  150. }
  151. } catch (Exception ex) {
  152. textView.Text = ex.Message;
  153. }
  154. textView.SetNeedsDisplay ();
  155. }
  156. private bool CanExpand (object arg)
  157. {
  158. return arg is Assembly || arg is Type || arg is ShowForType;
  159. }
  160. private IEnumerable<object> ChildGetter (object arg)
  161. {
  162. try {
  163. if (arg is Assembly a) {
  164. return a.GetTypes ();
  165. }
  166. if (arg is Type t) {
  167. // Note that here we cannot simply return the enum values as the same object cannot appear under multiple branches
  168. return Enum.GetValues (typeof (Showable))
  169. .Cast<Showable> ()
  170. // Although we new the Type every time the delegate is called state is preserved because the class has appropriate equality members
  171. .Select (v => new ShowForType (v, t));
  172. }
  173. if (arg is ShowForType show) {
  174. switch (show.ToShow) {
  175. case Showable.Properties:
  176. return show.Type.GetProperties (GetFlags ());
  177. case Showable.Constructors:
  178. return show.Type.GetConstructors (GetFlags ());
  179. case Showable.Events:
  180. return show.Type.GetEvents (GetFlags ());
  181. case Showable.Fields:
  182. return show.Type.GetFields (GetFlags ());
  183. case Showable.Methods:
  184. return show.Type.GetMethods (GetFlags ());
  185. }
  186. }
  187. } catch (Exception) {
  188. return Enumerable.Empty<object> ();
  189. }
  190. return Enumerable.Empty<object> ();
  191. }
  192. private string GetRepresentation (object model)
  193. {
  194. try {
  195. if (model is Assembly ass) {
  196. return ass.GetName ().Name;
  197. }
  198. if (model is PropertyInfo p) {
  199. return p.Name;
  200. }
  201. if (model is FieldInfo f) {
  202. return f.Name;
  203. }
  204. if (model is EventInfo ei) {
  205. return ei.Name;
  206. }
  207. } catch (Exception ex) {
  208. return ex.Message;
  209. }
  210. return model.ToString ();
  211. }
  212. private void Quit ()
  213. {
  214. Application.RequestStop ();
  215. }
  216. }
  217. }