ClassExplorer.cs 6.7 KB

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