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 (t.IsValueType ? "struct" : "class");
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) {
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));
59 foreach (ConstructorInfo ci in t.GetConstructors ()) {
60 OutlineConstructor (ci);
67 foreach (MethodInfo m in Comparer.Sort (t.GetMethods ())) {
68 if ((m.Attributes & MethodAttributes.SpecialName) != 0)
78 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties ())) {
86 foreach (EventInfo ei in Comparer.Sort (t.GetEvents ())) {
93 o.Indent--; o.WriteLine ("}");
96 void OutlineEvent (EventInfo ei)
98 MethodBase accessor = ei.GetAddMethod ();
100 o.Write (GetMethodVisibility (accessor));
102 o.Write (FormatType (ei.EventHandlerType));
108 void OutlineConstructor (ConstructorInfo ci)
110 o.Write (GetMethodVisibility (ci));
113 OutlineParams (ci.GetParameters ());
118 void OutlineProperty (PropertyInfo pi)
120 ParameterInfo [] idxp = pi.GetIndexParameters ();
121 MethodBase accessor = pi.CanRead ? pi.GetGetMethod () : pi.GetSetMethod ();
123 o.Write (GetMethodVisibility (accessor));
124 o.Write (GetMethodModifiers (accessor));
125 o.Write (FormatType (pi.PropertyType));
128 if (idxp.Length == 0)
132 OutlineParams (idxp);
139 if (pi.CanRead) o.WriteLine ("get;");
140 if (pi.CanWrite) o.WriteLine ("set;");
146 void OutlineMethod (MethodInfo mi)
148 o.Write (GetMethodVisibility (mi));
149 o.Write (GetMethodModifiers (mi));
150 o.Write (FormatType (mi.ReturnType));
154 OutlineParams (mi.GetParameters ());
158 void OutlineParams (ParameterInfo [] pi)
161 foreach (ParameterInfo p in pi) {
162 bool isPointer = false;
163 if (p.ParameterType.IsByRef) {
164 o.Write (p.IsOut ? "out " : "ref ");
165 o.Write (FormatType (p.ParameterType.GetElementType ()));
167 o.Write (FormatType (p.ParameterType));
171 if (i + 1 < pi.Length)
177 static string GetMethodVisibility (MethodBase m)
179 if (m.IsPublic) return "public ";
180 if (m.IsFamily) return "protected ";
181 if (m.IsPrivate) return "private ";
182 if (m.IsAssembly) return "internal ";
187 static string GetMethodModifiers (MethodBase method)
192 if (method.IsVirtual)
193 return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
200 static string GetTypeVisibility (Type t)
202 switch (t.Attributes & TypeAttributes.VisibilityMask){
203 case TypeAttributes.Public:
204 case TypeAttributes.NestedPublic:
207 case TypeAttributes.NestedFamily:
208 case TypeAttributes.NestedFamANDAssem:
209 case TypeAttributes.NestedFamORAssem:
217 static string FormatType (Type t)
219 string type = t.FullName;
220 if (!type.StartsWith ("System."))
223 if (t.HasElementType) {
224 Type et = t.GetElementType ();
226 return FormatType (et) + " []";
228 return FormatType (et) + " *";
230 return "ref " + FormatType (et);
234 case "System.Byte": return "byte";
235 case "System.SByte": return "sbyte";
236 case "System.Int16": return "short";
237 case "System.Int32": return "int";
238 case "System.Int64": return "long";
240 case "System.UInt16": return "ushort";
241 case "System.UInt32": return "uint";
242 case "System.UInt64": return "ulong";
244 case "System.Single": return "float";
245 case "System.Double": return "double";
246 case "System.Decimal": return "decimal";
247 case "System.Boolean": return "bool";
248 case "System.Char": return "char";
249 case "System.String": return "string";
251 case "System.Object": return "object";
252 case "System.Void": return "void";
255 if (type.LastIndexOf(".") == 6)
256 return type.Substring(7);
262 public class Comparer : IComparer {
263 delegate int ComparerFunc (object a, object b);
267 Comparer (ComparerFunc f)
272 public int Compare (object a, object b)
277 static int CompareMemberInfo (object a, object b)
279 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
282 static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
284 public static MemberInfo [] Sort (MemberInfo [] inf)
286 Array.Sort (inf, MemberInfoComparer);
290 static int CompareMethodBase (object a, object b)
292 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
294 if (aa.IsStatic == bb.IsStatic)
295 return CompareMemberInfo (a, b);
303 static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
305 public static MethodBase [] Sort (MethodBase [] inf)
307 Array.Sort (inf, MethodBaseComparer);
311 static int ComparePropertyInfo (object a, object b)
313 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
315 bool astatic = (aa.CanRead ? aa.GetGetMethod () : aa.GetSetMethod ()).IsStatic;
316 bool bstatic = (bb.CanRead ? bb.GetGetMethod () : bb.GetSetMethod ()).IsStatic;
318 if (astatic == bstatic)
319 return CompareMemberInfo (a, b);
327 static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
329 public static PropertyInfo [] Sort (PropertyInfo [] inf)
331 Array.Sort (inf, PropertyInfoComparer);
335 static int CompareEventInfo (object a, object b)
337 EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
339 bool astatic = aa.GetAddMethod (true).IsStatic;
340 bool bstatic = bb.GetAddMethod (true).IsStatic;
342 if (astatic == bstatic)
343 return CompareMemberInfo (a, b);
351 static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
353 public static EventInfo [] Sort (EventInfo [] inf)
355 Array.Sort (inf, EventInfoComparer);