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
)
31 o
.Write (GetTypeVisibility (t
));
33 if (t
.IsClass
&& !t
.IsSubclassOf (typeof (System
.MulticastDelegate
))) {
35 o
.Write (t
.IsAbstract
? " static" : " sealed");
36 else if (t
.IsAbstract
)
37 o
.Write (" abstract");
41 o
.Write (GetTypeKind (t
));
44 Type
[] interfaces
= (Type
[]) Comparer
.Sort (t
.GetInterfaces ());
45 Type parent
= t
.BaseType
;
47 if (t
.IsSubclassOf (typeof (System
.MulticastDelegate
))) {
50 method
= t
.GetMethod ("Invoke");
52 o
.Write (FormatType (method
.ReturnType
));
56 OutlineParams (method
.GetParameters ());
63 if (((parent
!= null && parent
!= typeof (object) && parent
!= typeof (ValueType
)) || interfaces
.Length
!= 0) && ! t
.IsEnum
) {
67 if (parent
!= null && parent
!= typeof (object) && parent
!= typeof (ValueType
)) {
68 o
.Write (FormatType (parent
));
72 foreach (Type intf
in interfaces
) {
73 if (!first
) o
.Write (", ");
76 o
.Write (FormatType (intf
));
85 foreach (FieldInfo fi
in t
.GetFields (BindingFlags
.Public
| BindingFlags
.Static
)) {
93 o
.Indent
--; o
.WriteLine ("}");
97 foreach (ConstructorInfo ci
in t
.GetConstructors (flags
)) {
98 OutlineConstructor (ci
);
105 foreach (MethodInfo m
in Comparer
.Sort (t
.GetMethods (flags
))) {
106 if ((m
.Attributes
& MethodAttributes
.SpecialName
) != 0)
116 foreach (PropertyInfo pi
in Comparer
.Sort (t
.GetProperties (flags
))) {
117 OutlineProperty (pi
);
124 foreach (FieldInfo fi
in t
.GetFields (flags
))
131 foreach (EventInfo ei
in Comparer
.Sort (t
.GetEvents (flags
))) {
139 foreach (Type ntype
in Comparer
.Sort (t
.GetNestedTypes (flags
)))
140 new Outline (ntype
, o
).OutlineType (flags
);
142 o
.Indent
--; o
.WriteLine ("}");
145 // FIXME: add other interesting attributes?
146 void OutlineAttributes ()
148 if (t
.IsSerializable
)
149 o
.WriteLine ("[Serializable]");
151 if (t
.IsDefined (typeof (System
.FlagsAttribute
), true))
152 o
.WriteLine ("[Flags]");
154 if (t
.IsDefined (typeof (System
.ObsoleteAttribute
), true))
155 o
.WriteLine ("[Obsolete]");
158 void OutlineEvent (EventInfo ei
)
160 MethodBase accessor
= ei
.GetAddMethod ();
162 o
.Write (GetMethodVisibility (accessor
));
164 o
.Write (FormatType (ei
.EventHandlerType
));
170 void OutlineConstructor (ConstructorInfo ci
)
172 o
.Write (GetMethodVisibility (ci
));
175 OutlineParams (ci
.GetParameters ());
180 void OutlineProperty (PropertyInfo pi
)
182 ParameterInfo
[] idxp
= pi
.GetIndexParameters ();
183 MethodBase accessor
= pi
.CanRead
? pi
.GetGetMethod (true) : pi
.GetSetMethod (true);
185 o
.Write (GetMethodVisibility (accessor
));
186 o
.Write (GetMethodModifiers (accessor
));
187 o
.Write (FormatType (pi
.PropertyType
));
190 if (idxp
.Length
== 0)
194 OutlineParams (idxp
);
201 if (pi
.CanRead
) o
.WriteLine ("get;");
202 if (pi
.CanWrite
) o
.WriteLine ("set;");
208 void OutlineMethod (MethodInfo mi
)
210 o
.Write (GetMethodVisibility (mi
));
211 o
.Write (GetMethodModifiers (mi
));
212 o
.Write (FormatType (mi
.ReturnType
));
216 OutlineParams (mi
.GetParameters ());
220 void OutlineParams (ParameterInfo
[] pi
)
223 foreach (ParameterInfo p
in pi
) {
224 if (p
.ParameterType
.IsByRef
) {
225 o
.Write (p
.IsOut
? "out " : "ref ");
226 o
.Write (FormatType (p
.ParameterType
.GetElementType ()));
227 } else if (p
.IsDefined (typeof (ParamArrayAttribute
), false)) {
229 o
.Write (FormatType (p
.ParameterType
));
231 o
.Write (FormatType (p
.ParameterType
));
236 if (i
+ 1 < pi
.Length
)
242 void OutlineField (FieldInfo fi
)
244 if (fi
.IsPublic
) o
.Write ("public ");
245 if (fi
.IsFamily
) o
.Write ("protected ");
246 if (fi
.IsPrivate
) o
.Write ("private ");
247 if (fi
.IsAssembly
) o
.Write ("internal ");
248 if (fi
.IsLiteral
) o
.Write ("const ");
249 o
.Write (FormatType (fi
.FieldType
));
255 o
.Write (fi
.GetValue (this));
260 static string GetMethodVisibility (MethodBase m
)
262 if (m
.IsPublic
) return "public ";
263 if (m
.IsFamily
) return "protected ";
264 if (m
.IsPrivate
) return "private ";
265 if (m
.IsAssembly
) return "internal ";
270 static string GetMethodModifiers (MethodBase method
)
275 if (method
.IsVirtual
)
276 return ((method
.Attributes
& MethodAttributes
.NewSlot
) != 0) ?
283 static string GetTypeKind (Type t
)
288 if (t
.IsSubclassOf (typeof (System
.MulticastDelegate
)))
300 static string GetTypeVisibility (Type t
)
302 switch (t
.Attributes
& TypeAttributes
.VisibilityMask
){
303 case TypeAttributes
.Public
:
304 case TypeAttributes
.NestedPublic
:
307 case TypeAttributes
.NestedFamily
:
308 case TypeAttributes
.NestedFamANDAssem
:
309 case TypeAttributes
.NestedFamORAssem
:
317 static string FormatType (Type t
)
319 string type
= t
.FullName
;
320 if (!type
.StartsWith ("System."))
323 if (t
.HasElementType
) {
324 Type et
= t
.GetElementType ();
326 return FormatType (et
) + " []";
328 return FormatType (et
) + " *";
330 return "ref " + FormatType (et
);
334 case "System.Byte": return "byte";
335 case "System.SByte": return "sbyte";
336 case "System.Int16": return "short";
337 case "System.Int32": return "int";
338 case "System.Int64": return "long";
340 case "System.UInt16": return "ushort";
341 case "System.UInt32": return "uint";
342 case "System.UInt64": return "ulong";
344 case "System.Single": return "float";
345 case "System.Double": return "double";
346 case "System.Decimal": return "decimal";
347 case "System.Boolean": return "bool";
348 case "System.Char": return "char";
349 case "System.String": return "string";
351 case "System.Object": return "object";
352 case "System.Void": return "void";
355 if (type
.LastIndexOf(".") == 6)
356 return type
.Substring(7);
362 public class Comparer
: IComparer
{
363 delegate int ComparerFunc (object a
, object b
);
367 Comparer (ComparerFunc f
)
372 public int Compare (object a
, object b
)
377 static int CompareType (object a
, object b
)
379 Type type1
= (Type
) a
;
380 Type type2
= (Type
) b
;
382 if (type1
.IsSubclassOf (typeof (System
.MulticastDelegate
)) != type2
.IsSubclassOf (typeof (System
.MulticastDelegate
)))
383 return (type1
.IsSubclassOf (typeof (System
.MulticastDelegate
)))? -1:1;
384 return string.Compare (type1
.Name
, type2
.Name
);
388 static Comparer TypeComparer
= new Comparer (new ComparerFunc (CompareType
));
390 static Type
[] Sort (Type
[] types
)
392 Array
.Sort (types
, TypeComparer
);
396 static int CompareMemberInfo (object a
, object b
)
398 return string.Compare (((MemberInfo
) a
).Name
, ((MemberInfo
) b
).Name
);
401 static Comparer MemberInfoComparer
= new Comparer (new ComparerFunc (CompareMemberInfo
));
403 public static MemberInfo
[] Sort (MemberInfo
[] inf
)
405 Array
.Sort (inf
, MemberInfoComparer
);
409 static int CompareMethodBase (object a
, object b
)
411 MethodBase aa
= (MethodBase
) a
, bb
= (MethodBase
) b
;
413 if (aa
.IsStatic
== bb
.IsStatic
)
414 return CompareMemberInfo (a
, b
);
422 static Comparer MethodBaseComparer
= new Comparer (new ComparerFunc (CompareMethodBase
));
424 public static MethodBase
[] Sort (MethodBase
[] inf
)
426 Array
.Sort (inf
, MethodBaseComparer
);
430 static int ComparePropertyInfo (object a
, object b
)
432 PropertyInfo aa
= (PropertyInfo
) a
, bb
= (PropertyInfo
) b
;
434 bool astatic
= (aa
.CanRead
? aa
.GetGetMethod (true) : aa
.GetSetMethod (true)).IsStatic
;
435 bool bstatic
= (bb
.CanRead
? bb
.GetGetMethod (true) : bb
.GetSetMethod (true)).IsStatic
;
437 if (astatic
== bstatic
)
438 return CompareMemberInfo (a
, b
);
446 static Comparer PropertyInfoComparer
= new Comparer (new ComparerFunc (ComparePropertyInfo
));
448 public static PropertyInfo
[] Sort (PropertyInfo
[] inf
)
450 Array
.Sort (inf
, PropertyInfoComparer
);
454 static int CompareEventInfo (object a
, object b
)
456 EventInfo aa
= (EventInfo
) a
, bb
= (EventInfo
) b
;
458 bool astatic
= aa
.GetAddMethod (true).IsStatic
;
459 bool bstatic
= bb
.GetAddMethod (true).IsStatic
;
461 if (astatic
== bstatic
)
462 return CompareMemberInfo (a
, b
);
470 static Comparer EventInfoComparer
= new Comparer (new ComparerFunc (CompareEventInfo
));
472 public static EventInfo
[] Sort (EventInfo
[] inf
)
474 Array
.Sort (inf
, EventInfoComparer
);