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 ()
30 o.Write (GetTypeVisibility (t));
32 o.Write (GetTypeKind (t));
36 Type [] interfaces = (Type []) Comparer.Sort (t.GetInterfaces ());
37 Type parent = t.BaseType;
39 if (((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) && ! t.IsEnum) {
43 if (parent != null && parent != typeof (object) && parent != typeof (ValueType)) {
44 o.Write (FormatType (parent));
48 foreach (Type intf in interfaces) {
49 if (!first) o.Write (", ");
52 o.Write (FormatType (intf));
61 foreach (FieldInfo fi in t.GetFields (BindingFlags.Public | BindingFlags.Static)) {
69 o.Indent--; o.WriteLine ("}");
73 foreach (ConstructorInfo ci in t.GetConstructors ()) {
74 OutlineConstructor (ci);
81 foreach (MethodInfo m in Comparer.Sort (t.GetMethods ())) {
82 if ((m.Attributes & MethodAttributes.SpecialName) != 0)
92 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties ())) {
100 foreach (EventInfo ei in Comparer.Sort (t.GetEvents ())) {
107 o.Indent--; o.WriteLine ("}");
110 void OutlineEvent (EventInfo ei)
112 MethodBase accessor = ei.GetAddMethod ();
114 o.Write (GetMethodVisibility (accessor));
116 o.Write (FormatType (ei.EventHandlerType));
122 void OutlineConstructor (ConstructorInfo ci)
124 o.Write (GetMethodVisibility (ci));
127 OutlineParams (ci.GetParameters ());
132 void OutlineProperty (PropertyInfo pi)
134 ParameterInfo [] idxp = pi.GetIndexParameters ();
135 MethodBase accessor = pi.CanRead ? pi.GetGetMethod (true) : pi.GetSetMethod (true);
137 o.Write (GetMethodVisibility (accessor));
138 o.Write (GetMethodModifiers (accessor));
139 o.Write (FormatType (pi.PropertyType));
142 if (idxp.Length == 0)
146 OutlineParams (idxp);
153 if (pi.CanRead) o.WriteLine ("get;");
154 if (pi.CanWrite) o.WriteLine ("set;");
160 void OutlineMethod (MethodInfo mi)
162 o.Write (GetMethodVisibility (mi));
163 o.Write (GetMethodModifiers (mi));
164 o.Write (FormatType (mi.ReturnType));
168 OutlineParams (mi.GetParameters ());
172 void OutlineParams (ParameterInfo [] pi)
175 foreach (ParameterInfo p in pi) {
176 bool isPointer = false;
177 if (p.ParameterType.IsByRef) {
178 o.Write (p.IsOut ? "out " : "ref ");
179 o.Write (FormatType (p.ParameterType.GetElementType ()));
181 o.Write (FormatType (p.ParameterType));
185 if (i + 1 < pi.Length)
191 static string GetMethodVisibility (MethodBase m)
193 if (m.IsPublic) return "public ";
194 if (m.IsFamily) return "protected ";
195 if (m.IsPrivate) return "private ";
196 if (m.IsAssembly) return "internal ";
201 static string GetMethodModifiers (MethodBase method)
206 if (method.IsVirtual)
207 return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
214 static string GetTypeKind (Type t)
227 static string GetTypeVisibility (Type t)
229 switch (t.Attributes & TypeAttributes.VisibilityMask){
230 case TypeAttributes.Public:
231 case TypeAttributes.NestedPublic:
234 case TypeAttributes.NestedFamily:
235 case TypeAttributes.NestedFamANDAssem:
236 case TypeAttributes.NestedFamORAssem:
244 static string FormatType (Type t)
246 string type = t.FullName;
247 if (!type.StartsWith ("System."))
250 if (t.HasElementType) {
251 Type et = t.GetElementType ();
253 return FormatType (et) + " []";
255 return FormatType (et) + " *";
257 return "ref " + FormatType (et);
261 case "System.Byte": return "byte";
262 case "System.SByte": return "sbyte";
263 case "System.Int16": return "short";
264 case "System.Int32": return "int";
265 case "System.Int64": return "long";
267 case "System.UInt16": return "ushort";
268 case "System.UInt32": return "uint";
269 case "System.UInt64": return "ulong";
271 case "System.Single": return "float";
272 case "System.Double": return "double";
273 case "System.Decimal": return "decimal";
274 case "System.Boolean": return "bool";
275 case "System.Char": return "char";
276 case "System.String": return "string";
278 case "System.Object": return "object";
279 case "System.Void": return "void";
282 if (type.LastIndexOf(".") == 6)
283 return type.Substring(7);
289 public class Comparer : IComparer {
290 delegate int ComparerFunc (object a, object b);
294 Comparer (ComparerFunc f)
299 public int Compare (object a, object b)
304 static int CompareMemberInfo (object a, object b)
306 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
309 static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
311 public static MemberInfo [] Sort (MemberInfo [] inf)
313 Array.Sort (inf, MemberInfoComparer);
317 static int CompareMethodBase (object a, object b)
319 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
321 if (aa.IsStatic == bb.IsStatic)
322 return CompareMemberInfo (a, b);
330 static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
332 public static MethodBase [] Sort (MethodBase [] inf)
334 Array.Sort (inf, MethodBaseComparer);
338 static int ComparePropertyInfo (object a, object b)
340 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
342 bool astatic = (aa.CanRead ? aa.GetGetMethod (true) : aa.GetSetMethod (true)).IsStatic;
343 bool bstatic = (bb.CanRead ? bb.GetGetMethod (true) : bb.GetSetMethod (true)).IsStatic;
345 if (astatic == bstatic)
346 return CompareMemberInfo (a, b);
354 static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
356 public static PropertyInfo [] Sort (PropertyInfo [] inf)
358 Array.Sort (inf, PropertyInfoComparer);
362 static int CompareEventInfo (object a, object b)
364 EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
366 bool astatic = aa.GetAddMethod (true).IsStatic;
367 bool bstatic = bb.GetAddMethod (true).IsStatic;
369 if (astatic == bstatic)
370 return CompareMemberInfo (a, b);
378 static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
380 public static EventInfo [] Sort (EventInfo [] inf)
382 Array.Sort (inf, EventInfoComparer);