2 // monop -- a semi-clone of javap
5 // Ben Maurer (bmaurer@users.sourceforge.net)
12 using System.Reflection;
13 using System.Collections;
14 using System.CodeDom.Compiler;
18 static string assembly;
19 static BindingFlags default_flags =
20 BindingFlags.Instance |
24 // very common namespaces, all in corlib
25 static readonly string [] v_common_ns = {
33 static readonly string [] common_assemblies = {
40 static readonly string [] common_ns = {
47 static Type GetType (string tname, bool ignoreCase)
50 if (assembly != null) {
51 Assembly a = GetAssembly (assembly, true);
52 t = a.GetType (tname, false, ignoreCase);
54 t = Type.GetType (tname, false, ignoreCase);
60 static Assembly GetAssembly (string assembly, bool exit)
65 // if is starts with / use the full path
66 // otherwise try to load from the GAC
67 if (assembly.StartsWith ("/"))
68 a = Assembly.LoadFrom (assembly);
70 a = Assembly.LoadWithPartialName (assembly);
72 // if the above failed try Load
74 a = Assembly.Load (assembly);
80 Console.WriteLine ("Could not load {0}", MonoP.assembly);
87 static Type GetType (string tname)
89 return GetType (tname, false);
92 static void PrintTypes (string assembly)
94 Assembly a = GetAssembly (assembly, true);
95 Type [] types = a.GetExportedTypes ();
97 foreach (Type t in types)
98 Console.WriteLine (t.FullName);
100 Console.WriteLine ("\nTotal: {0} types.", types.Length);
103 static void Completion (string prefix)
105 foreach (Type t in typeof (object).Assembly.GetExportedTypes ()) {
106 if (t.Name.StartsWith (prefix)) {
107 if (Array.IndexOf (v_common_ns, t.Namespace) != -1) {
108 Console.WriteLine (t.Name);
113 if (t.FullName.StartsWith (prefix)) {
114 Console.WriteLine (t.FullName);
118 foreach (string assm in common_assemblies) {
121 Assembly a = GetAssembly (assm, true);
122 foreach (Type t in a.GetExportedTypes ()) {
124 if (t.Name.StartsWith (prefix)) {
125 if (Array.IndexOf (common_ns, t.Namespace) != -1) {
126 Console.WriteLine (t.Name);
131 if (t.FullName.StartsWith (prefix)) {
132 Console.WriteLine (t.FullName);
142 static void Main (string [] args)
144 if (args.Length < 1) {
145 Console.WriteLine ("monop [-c] [-r:Assembly] [class-name]");
149 IndentedTextWriter o = new IndentedTextWriter (Console.Out, " ");
152 if (args [0].StartsWith ("-r:") || args [0].StartsWith ("/r:")){
154 assembly = args [0].Substring (3);
156 if (args.Length == 1) {
157 PrintTypes (assembly);
162 if (args [0] == "-c") {
163 Completion (args [1]);
167 if (args [i] == "--private" || args [i] == "-p") {
168 default_flags |= BindingFlags.NonPublic;
172 if (args.Length < i+1){
173 Console.WriteLine ("Usage is: monop [-r:Assembly] [class-name]");
177 string tname = args [i];
178 Type t = GetType (tname);
181 // Try some very common ones, dont load anything
182 foreach (string ns in v_common_ns) {
183 t = GetType (ns + "." + tname, true);
190 foreach (string assm in common_assemblies) {
192 Assembly a = GetAssembly (assm, false);
193 t = a.GetType (tname, false, true);
196 foreach (string ns in common_ns) {
197 t = a.GetType (ns + "." + tname, false, true);
199 Console.WriteLine ("(using class from {0})", ns);
209 Console.WriteLine ("Could not find {0}", tname);
214 // This gets us nice buffering
216 StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
217 new Outline (t, sw).OutlineType (default_flags);