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 ()) {
62 if (fi.Name == "value__")
71 o.Indent--; o.WriteLine ("}");
75 foreach (ConstructorInfo ci in t.GetConstructors ()) {
76 OutlineConstructor (ci);
83 foreach (MethodInfo m in Comparer.Sort (t.GetMethods ())) {
84 if ((m.Attributes & MethodAttributes.SpecialName) != 0)
94 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties ())) {
102 foreach (EventInfo ei in Comparer.Sort (t.GetEvents ())) {
109 o.Indent--; o.WriteLine ("}");
112 void OutlineEvent (EventInfo ei)
114 MethodBase accessor = ei.GetAddMethod ();
116 o.Write (GetMethodVisibility (accessor));
118 o.Write (FormatType (ei.EventHandlerType));
124 void OutlineConstructor (ConstructorInfo ci)
126 o.Write (GetMethodVisibility (ci));
129 OutlineParams (ci.GetParameters ());
134 void OutlineProperty (PropertyInfo pi)
136 ParameterInfo [] idxp = pi.GetIndexParameters ();
137 MethodBase accessor = pi.CanRead ? pi.GetGetMethod () : pi.GetSetMethod ();
139 o.Write (GetMethodVisibility (accessor));
140 o.Write (GetMethodModifiers (accessor));
141 o.Write (FormatType (pi.PropertyType));
144 if (idxp.Length == 0)
148 OutlineParams (idxp);
155 if (pi.CanRead) o.WriteLine ("get;");
156 if (pi.CanWrite) o.WriteLine ("set;");
162 void OutlineMethod (MethodInfo mi)
164 o.Write (GetMethodVisibility (mi));
165 o.Write (GetMethodModifiers (mi));
166 o.Write (FormatType (mi.ReturnType));
170 OutlineParams (mi.GetParameters ());
174 void OutlineParams (ParameterInfo [] pi)
177 foreach (ParameterInfo p in pi) {
178 bool isPointer = false;
179 if (p.ParameterType.IsByRef) {
180 o.Write (p.IsOut ? "out " : "ref ");
181 o.Write (FormatType (p.ParameterType.GetElementType ()));
183 o.Write (FormatType (p.ParameterType));
187 if (i + 1 < pi.Length)
193 static string GetMethodVisibility (MethodBase m)
195 if (m.IsPublic) return "public ";
196 if (m.IsFamily) return "protected ";
197 if (m.IsPrivate) return "private ";
198 if (m.IsAssembly) return "internal ";
203 static string GetMethodModifiers (MethodBase method)
208 if (method.IsVirtual)
209 return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
216 static string GetTypeKind (Type t)
229 static string GetTypeVisibility (Type t)
231 switch (t.Attributes & TypeAttributes.VisibilityMask){
232 case TypeAttributes.Public:
233 case TypeAttributes.NestedPublic:
236 case TypeAttributes.NestedFamily:
237 case TypeAttributes.NestedFamANDAssem:
238 case TypeAttributes.NestedFamORAssem:
246 static string FormatType (Type t)
248 string type = t.FullName;
249 if (!type.StartsWith ("System."))
252 if (t.HasElementType) {
253 Type et = t.GetElementType ();
255 return FormatType (et) + " []";
257 return FormatType (et) + " *";
259 return "ref " + FormatType (et);
263 case "System.Byte": return "byte";
264 case "System.SByte": return "sbyte";
265 case "System.Int16": return "short";
266 case "System.Int32": return "int";
267 case "System.Int64": return "long";
269 case "System.UInt16": return "ushort";
270 case "System.UInt32": return "uint";
271 case "System.UInt64": return "ulong";
273 case "System.Single": return "float";
274 case "System.Double": return "double";
275 case "System.Decimal": return "decimal";
276 case "System.Boolean": return "bool";
277 case "System.Char": return "char";
278 case "System.String": return "string";
280 case "System.Object": return "object";
281 case "System.Void": return "void";
284 if (type.LastIndexOf(".") == 6)
285 return type.Substring(7);
291 public class Comparer : IComparer {
292 delegate int ComparerFunc (object a, object b);
296 Comparer (ComparerFunc f)
301 public int Compare (object a, object b)
306 static int CompareMemberInfo (object a, object b)
308 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
311 static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
313 public static MemberInfo [] Sort (MemberInfo [] inf)
315 Array.Sort (inf, MemberInfoComparer);
319 static int CompareMethodBase (object a, object b)
321 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
323 if (aa.IsStatic == bb.IsStatic)
324 return CompareMemberInfo (a, b);
332 static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
334 public static MethodBase [] Sort (MethodBase [] inf)
336 Array.Sort (inf, MethodBaseComparer);
340 static int ComparePropertyInfo (object a, object b)
342 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
344 bool astatic = (aa.CanRead ? aa.GetGetMethod () : aa.GetSetMethod ()).IsStatic;
345 bool bstatic = (bb.CanRead ? bb.GetGetMethod () : bb.GetSetMethod ()).IsStatic;
347 if (astatic == bstatic)
348 return CompareMemberInfo (a, b);
356 static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
358 public static PropertyInfo [] Sort (PropertyInfo [] inf)
360 Array.Sort (inf, PropertyInfoComparer);
364 static int CompareEventInfo (object a, object b)
366 EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
368 bool astatic = aa.GetAddMethod (true).IsStatic;
369 bool bstatic = bb.GetAddMethod (true).IsStatic;
371 if (astatic == bstatic)
372 return CompareMemberInfo (a, b);
380 static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
382 public static EventInfo [] Sort (EventInfo [] inf)
384 Array.Sort (inf, EventInfoComparer);