3 // Finds types and (optionally) shows reflection information about
6 // Author: Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2002 Jonathan Pryor
14 using System.Collections;
16 using System.Diagnostics;
17 using System.Reflection;
19 using System.Text.RegularExpressions;
21 namespace Mono.TypeReflector
23 public class ConsoleOutput {
25 private static BooleanSwitch console = new BooleanSwitch ("console",
26 "console-specific and command-line handling output");
28 private static void TraceStringArray (string message, IEnumerable contents)
30 Trace.WriteLineIf (console.Enabled, message);
31 foreach (string s in contents) {
32 Trace.WriteLineIf (console.Enabled, " " + s);
36 private static void PrintVersion ()
38 Console.WriteLine ("type-reflector 0.5");
39 Console.WriteLine ("Written by Jonathan Pryor.");
41 Console.WriteLine ("Copyright (C) 2002 Jonathan Pryor.");
44 private static void InitFactory ()
46 // TypeDisplayerFactory.Add ("explicit", typeof(ExplicitTypeDisplayer));
47 // TypeDisplayerFactory.Add ("reflection", typeof(ReflectionTypeDisplayer));
48 // TypeDisplayerFactory.Add ("c#", typeof(CSharpTypeDisplayer));
49 Factories.FormatterFactory.Add ("default", typeof (DefaultNodeFormatter));
50 Factories.FormatterFactory.Add ("csharp", typeof (CSharpNodeFormatter));
51 Factories.FinderFactory.Add ("explicit", typeof (ExplicitNodeFinder));
52 Factories.FinderFactory.Add ("reflection", typeof (ReflectionNodeFinder));
55 public static void Main (string[] args)
59 TypeReflectorOptions options = new TypeReflectorOptions ();
62 options.ParseOptions (args);
63 } catch (Exception e) {
64 Console.WriteLine (e.Message);
65 Console.WriteLine ("See `{0} --help' for more information", ProgramOptions.ProgramName);
66 // Console.WriteLine ("** Full Message continues:\n" + e);
70 if (options.FoundHelp) {
71 Console.WriteLine (options.OptionsHelp);
75 if (options.DefaultAssemblies) {
76 Console.WriteLine ("The default search assemblies are:");
77 foreach (string s in TypeReflectorOptions.GetDefaultAssemblies ()) {
78 Console.WriteLine (" {0}", s);
83 if (options.Version) {
88 if (options.Types.Count == 0) {
89 Console.WriteLine ("No types specified.");
90 Console.WriteLine ("See `{0} --help' for more information", ProgramOptions.ProgramName);
94 TraceStringArray ("Search Assemblies: ", options.Assemblies);
95 TraceStringArray ("Search for Types: ", options.Types);
97 TypeLoader loader = new TypeLoader (options.Assemblies);
98 loader.MatchBase = options.MatchBase;
99 loader.MatchFullName = options.MatchFullName;
100 loader.MatchClassName = options.MatchClassName;
101 loader.MatchNamespace = options.MatchNamespace;
102 loader.MatchMethodReturnType = options.MatchReturnType;
104 IndentingTextWriter writer = new IndentingTextWriter (Console.Out);
106 int depth = options.MaxDepth;
108 INodeFormatter formatter = Factories.FormatterFactory.Create (options.Formatter);
109 if (formatter == null) {
110 Console.WriteLine ("Error: invalid formatter: " + options.Formatter);
114 NodeFinder f = (NodeFinder) Factories.FinderFactory.Create (options.Finder);
116 Console.WriteLine ("Error: invalid finder: " + options.Finder);
120 f.VerboseOutput = options.VerboseOutput;
121 f.ShowBase = options.ShowBase;
122 f.ShowConstructors = options.ShowConstructors;
123 f.ShowEvents = options.ShowEvents;
124 f.ShowFields = options.ShowFields;
125 f.ShowInterfaces = options.ShowInterfaces;
126 f.ShowMethods = options.ShowMethods;
127 f.ShowProperties = options.ShowProperties;
128 f.ShowTypeProperties = options.ShowTypeProperties;
129 f.ShowInheritedMembers = options.ShowInheritedMembers;
130 f.ShowNonPublic = options.ShowNonPublic;
131 f.ShowMonoBroken = options.ShowMonoBroken;
132 f.FlattenHierarchy = options.FlattenHierarchy;
133 f.MaxDepth = options.MaxDepth;
135 foreach (string t in options.Types) {
137 ICollection typesFound = loader.LoadTypes (t);
138 if (typesFound.Count > 0)
139 foreach (Type type in loader.LoadTypes(t)) {
140 // Console.WriteLine ("** displaying type: " + type);
141 Node root = new Node (formatter,
143 // new GroupingNodeFinder (f));
144 // new ExplicitNodeFinder());
145 // root.Extra = new NodeInfo (null, type, NodeTypes.Type);
146 root.NodeInfo = new NodeInfo (null, type);
147 ShowNode (root, writer, depth);
150 Console.WriteLine ("Unable to find type `{0}'.", t);
151 } catch (Exception e) {
152 Console.WriteLine ("Unable to display type `{0}': {1}.", t, e.ToString());
157 private static void ShowNode (Node root, IndentingTextWriter writer, int maxDepth)
159 // Console.WriteLine ("** current max depth: " + maxDepth);
160 writer.WriteLine (root.Description);
162 using (Indenter i = new Indenter (writer)) {
163 foreach (Node child in root.GetChildren()) {
164 ShowNode (child, writer, maxDepth-1);