2 // outline -- support for rendering in monop
3 // Some code stolen from updater.cs in monodoc.
6 // Ben Maurer (bmaurer@users.sourceforge.net)
12 using System.Reflection;
13 using System.Collections;
14 using System.CodeDom.Compiler;
17 public class Outline {
22 public Outline (Type t, TextWriter output)
25 this.o = new IndentedTextWriter (output, " ");
28 public void OutlineType (BindingFlags flags)
30 o.Write (GetTypeVisibility (t));
32 o.Write (GetTypeKind (t));
35 Type [] interfaces = (Type []) Comparer.Sort (t.GetInterfaces ());
36 Type parent = t.BaseType;
38 if (t.IsSubclassOf (typeof (System.MulticastDelegate))) {
41 method = t.GetMethod ("Invoke");
43 o.Write (FormatType (method.ReturnType));
47 OutlineParams (method.GetParameters ());
54 if (((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) && ! t.IsEnum) {
58 if (parent != null && parent != typeof (object) && parent != typeof (ValueType)) {
59 o.Write (FormatType (parent));
63 foreach (Type intf in interfaces) {
64 if (!first) o.Write (", ");
67 o.Write (FormatType (intf));
76 foreach (FieldInfo fi in t.GetFields (BindingFlags.Public | BindingFlags.Static)) {
84 o.Indent--; o.WriteLine ("}");
88 foreach (ConstructorInfo ci in t.GetConstructors (flags)) {
89 OutlineConstructor (ci);
96 foreach (MethodInfo m in Comparer.Sort (t.GetMethods (flags))) {
97 if ((m.Attributes & MethodAttributes.SpecialName) != 0)
107 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties (flags))) {
108 OutlineProperty (pi);
115 foreach (EventInfo ei in Comparer.Sort (t.GetEvents (flags))) {
123 foreach (Type ntype in Comparer.Sort (t.GetNestedTypes (flags)))
124 new Outline (ntype, o).OutlineType (flags);
126 o.Indent--; o.WriteLine ("}");
129 void OutlineEvent (EventInfo ei)
131 MethodBase accessor = ei.GetAddMethod ();
133 o.Write (GetMethodVisibility (accessor));
135 o.Write (FormatType (ei.EventHandlerType));
141 void OutlineConstructor (ConstructorInfo ci)
143 o.Write (GetMethodVisibility (ci));
146 OutlineParams (ci.GetParameters ());
151 void OutlineProperty (PropertyInfo pi)
153 ParameterInfo [] idxp = pi.GetIndexParameters ();
154 MethodBase accessor = pi.CanRead ? pi.GetGetMethod (true) : pi.GetSetMethod (true);
156 o.Write (GetMethodVisibility (accessor));
157 o.Write (GetMethodModifiers (accessor));
158 o.Write (FormatType (pi.PropertyType));
161 if (idxp.Length == 0)
165 OutlineParams (idxp);
172 if (pi.CanRead) o.WriteLine ("get;");
173 if (pi.CanWrite) o.WriteLine ("set;");
179 void OutlineMethod (MethodInfo mi)
181 o.Write (GetMethodVisibility (mi));
182 o.Write (GetMethodModifiers (mi));
183 o.Write (FormatType (mi.ReturnType));
187 OutlineParams (mi.GetParameters ());
191 void OutlineParams (ParameterInfo [] pi)
194 foreach (ParameterInfo p in pi) {
195 bool isPointer = false;
196 if (p.ParameterType.IsByRef) {
197 o.Write (p.IsOut ? "out " : "ref ");
198 o.Write (FormatType (p.ParameterType.GetElementType ()));
200 o.Write (FormatType (p.ParameterType));
204 if (i + 1 < pi.Length)
210 static string GetMethodVisibility (MethodBase m)
212 if (m.IsPublic) return "public ";
213 if (m.IsFamily) return "protected ";
214 if (m.IsPrivate) return "private ";
215 if (m.IsAssembly) return "internal ";
220 static string GetMethodModifiers (MethodBase method)
225 if (method.IsVirtual)
226 return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
233 static string GetTypeKind (Type t)
238 if (t.IsSubclassOf (typeof (System.MulticastDelegate)))
250 static string GetTypeVisibility (Type t)
252 switch (t.Attributes & TypeAttributes.VisibilityMask){
253 case TypeAttributes.Public:
254 case TypeAttributes.NestedPublic:
257 case TypeAttributes.NestedFamily:
258 case TypeAttributes.NestedFamANDAssem:
259 case TypeAttributes.NestedFamORAssem:
267 static string FormatType (Type t)
269 string type = t.FullName;
270 if (!type.StartsWith ("System."))
273 if (t.HasElementType) {
274 Type et = t.GetElementType ();
276 return FormatType (et) + " []";
278 return FormatType (et) + " *";
280 return "ref " + FormatType (et);
284 case "System.Byte": return "byte";
285 case "System.SByte": return "sbyte";
286 case "System.Int16": return "short";
287 case "System.Int32": return "int";
288 case "System.Int64": return "long";
290 case "System.UInt16": return "ushort";
291 case "System.UInt32": return "uint";
292 case "System.UInt64": return "ulong";
294 case "System.Single": return "float";
295 case "System.Double": return "double";
296 case "System.Decimal": return "decimal";
297 case "System.Boolean": return "bool";
298 case "System.Char": return "char";
299 case "System.String": return "string";
301 case "System.Object": return "object";
302 case "System.Void": return "void";
305 if (type.LastIndexOf(".") == 6)
306 return type.Substring(7);
312 public class Comparer : IComparer {
313 delegate int ComparerFunc (object a, object b);
317 Comparer (ComparerFunc f)
322 public int Compare (object a, object b)
327 static int CompareType (object a, object b)
329 Type type1 = (Type) a;
330 Type type2 = (Type) b;
332 if (type1.IsSubclassOf (typeof (System.MulticastDelegate)) != type2.IsSubclassOf (typeof (System.MulticastDelegate)))
333 return (type1.IsSubclassOf (typeof (System.MulticastDelegate)))? -1:1;
334 return string.Compare (type1.Name, type2.Name);
338 static Comparer TypeComparer = new Comparer (new ComparerFunc (CompareType));
340 static Type [] Sort (Type [] types)
342 Array.Sort (types, TypeComparer);
346 static int CompareMemberInfo (object a, object b)
348 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
351 static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
353 public static MemberInfo [] Sort (MemberInfo [] inf)
355 Array.Sort (inf, MemberInfoComparer);
359 static int CompareMethodBase (object a, object b)
361 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
363 if (aa.IsStatic == bb.IsStatic)
364 return CompareMemberInfo (a, b);
372 static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
374 public static MethodBase [] Sort (MethodBase [] inf)
376 Array.Sort (inf, MethodBaseComparer);
380 static int ComparePropertyInfo (object a, object b)
382 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
384 bool astatic = (aa.CanRead ? aa.GetGetMethod (true) : aa.GetSetMethod (true)).IsStatic;
385 bool bstatic = (bb.CanRead ? bb.GetGetMethod (true) : bb.GetSetMethod (true)).IsStatic;
387 if (astatic == bstatic)
388 return CompareMemberInfo (a, b);
396 static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
398 public static PropertyInfo [] Sort (PropertyInfo [] inf)
400 Array.Sort (inf, PropertyInfoComparer);
404 static int CompareEventInfo (object a, object b)
406 EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
408 bool astatic = aa.GetAddMethod (true).IsStatic;
409 bool bstatic = bb.GetAddMethod (true).IsStatic;
411 if (astatic == bstatic)
412 return CompareMemberInfo (a, b);
420 static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
422 public static EventInfo [] Sort (EventInfo [] inf)
424 Array.Sort (inf, EventInfoComparer);