2 // TestDisplayer.cs: Base class for parsing Type objects.
4 // Author: Jonathan Pryor (jonpryor@vt.edu)
6 // (C) 2002 Jonathan Pryor
8 // Permission is hereby granted, free of charge, to any
9 // person obtaining a copy of this software and associated
10 // documentation files (the "Software"), to deal in the
11 // Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software,
14 // and to permit persons to whom the Software is furnished to
15 // do so, subject to the following conditions:
17 // The above copyright notice and this permission notice
18 // shall be included in all copies or substantial portions
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
23 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
34 using System.Diagnostics;
35 using System.Reflection;
37 using System.Text.RegularExpressions;
39 namespace Mono.TypeReflector
41 public delegate void BaseTypeEventHandler (object sender, BaseTypeEventArgs e);
42 public delegate void TypeEventHandler (object sender, TypeEventArgs e);
43 public delegate void InterfacesEventHandler (object sender, InterfacesEventArgs e);
44 public delegate void FieldsEventHandler (object sender, FieldsEventArgs e);
45 public delegate void PropertiesEventHandler (object sender, PropertiesEventArgs e);
46 public delegate void EventsEventHandler (object sender, EventsEventArgs e);
47 public delegate void ConstructorsEventHandler (object sender, ConstructorsEventArgs e);
48 public delegate void MethodsEventHandler (object sender, MethodsEventArgs e);
50 public class BaseTypeEventArgs : EventArgs {
54 internal BaseTypeEventArgs (Type type)
59 public Type BaseType {
64 public class TypeEventArgs : EventArgs {
68 internal TypeEventArgs (Type type)
78 public class InterfacesEventArgs : EventArgs {
80 private Type[] _interfaces;
82 internal InterfacesEventArgs (Type[] interfaces)
84 _interfaces = interfaces ;
87 public Type[] Interfaces {
88 get {return _interfaces;}
92 public class FieldsEventArgs : EventArgs {
93 private FieldInfo[] _fields;
95 internal FieldsEventArgs (FieldInfo[] fields)
100 public FieldInfo[] Fields {
101 get {return _fields;}
105 public class PropertiesEventArgs : EventArgs {
107 private PropertyInfo[] _props;
109 internal PropertiesEventArgs (PropertyInfo[] properties)
114 public PropertyInfo[] Properties {
119 public class EventsEventArgs : EventArgs {
121 private EventInfo[] _events;
123 internal EventsEventArgs (EventInfo[] events)
128 public EventInfo[] Events {
129 get {return _events;}
133 public class ConstructorsEventArgs : EventArgs {
135 private ConstructorInfo[] _ctors;
137 internal ConstructorsEventArgs (ConstructorInfo[] ctors)
142 public ConstructorInfo[] Constructors {
147 public class MethodsEventArgs : EventArgs {
149 private MethodInfo[] _methods;
151 internal MethodsEventArgs (MethodInfo[] methods)
156 public MethodInfo[] Methods {
157 get {return _methods;}
161 public class TypeDisplayer {
163 private bool showBase = false;
164 private bool showConstructors = false;
165 private bool showEvents = false;
166 private bool showFields = false;
167 private bool showInterfaces = false;
168 private bool showMethods = false;
169 private bool showProperties = false;
170 private bool showTypeProperties = false;
171 private bool showInheritedMembers = false;
172 private bool verboseOutput = false;
173 private bool flattenHierarchy = false;
174 private bool showNonPublic = false;
175 private bool showMonoBroken = false;
177 // `PrintTypeProperties' is recursive, but refrains from printing
178 // duplicates. Despite duplicate removal, the output for printing the
179 // Properties of System.Type is > 800K of text.
181 // 3 levels permits viewing Attribute values, but not the attributes of
182 // those attribute values.
184 // For example, 3 levels permits:
185 // class System.Type {depth 0}
186 // Properties: {depth 1}
187 // System.Reflection.MemberTypes MemberType {depth 2}
188 // - CanRead=True {depth 3}
189 // - CanWrite=False {depth 3}
191 private int maxDepth = 3;
193 public TypeDisplayer ()
197 public int MaxDepth {
198 get {return maxDepth;}
199 set {maxDepth = value;}
202 public bool ShowBase {
203 get {return showBase;}
204 set {showBase = value;}
207 public bool ShowConstructors {
208 get {return showConstructors;}
209 set {showConstructors = value;}
212 public bool ShowEvents {
213 get {return showEvents;}
214 set {showEvents = value;}
217 public bool ShowFields {
218 get {return showFields;}
219 set {showFields = value;}
222 public bool ShowInterfaces {
223 get {return showInterfaces;}
224 set {showInterfaces = value;}
227 public bool ShowMethods {
228 get {return showMethods;}
229 set {showMethods = value;}
232 public bool ShowProperties {
233 get {return showProperties;}
234 set {showProperties = value;}
237 public bool ShowTypeProperties {
238 get {return showTypeProperties;}
239 set {showTypeProperties = value;}
242 public bool ShowInheritedMembers {
243 get {return showInheritedMembers;}
244 set {showInheritedMembers = value;}
247 public bool ShowNonPublic {
248 get {return showNonPublic;}
249 set {showNonPublic = value;}
252 public bool ShowMonoBroken {
253 get {return showMonoBroken;}
254 set {showMonoBroken = value;}
257 public bool FlattenHierarchy {
258 get {return flattenHierarchy;}
259 set {flattenHierarchy = value;}
262 public bool VerboseOutput {
263 get {return verboseOutput;}
264 set {verboseOutput = value;}
267 private static BindingFlags bindingFlags =
268 BindingFlags.DeclaredOnly |
269 BindingFlags.Public |
270 BindingFlags.Instance |
273 public void Parse (Type type)
275 BindingFlags bf = bindingFlags;
277 if (FlattenHierarchy)
278 bf |= BindingFlags.FlattenHierarchy;
279 if (ShowInheritedMembers)
280 bf &= ~BindingFlags.DeclaredOnly;
282 bf |= BindingFlags.NonPublic;
284 OnTypeDeclaration (type);
285 OnTypeBody (type, bf);
288 public static string FieldValue (FieldInfo f)
291 if (f.DeclaringType.IsEnum)
292 s = String.Format ("0x{0}",
293 Enum.Format (f.DeclaringType, f.GetValue (null), "x"));
295 s = f.GetValue(null).ToString();
299 protected virtual void OnTypeDeclaration (Type type)
303 BaseType (type.BaseType);
304 Interfaces (type.GetInterfaces ());
307 protected virtual void OnTypeBody (Type type, BindingFlags bf)
309 Fields (type.GetFields(bf));
310 Constructors (type.GetConstructors(bf));
311 Properties (type.GetProperties(bf));
312 Events (type.GetEvents(bf));
313 Methods (type.GetMethods(bf));
316 private void Type (Type t)
318 TypeEventArgs ea = new TypeEventArgs (t);
322 if (ReceiveTypes != null)
323 ReceiveTypes (this, ea);
327 protected virtual void OnType (TypeEventArgs e) {}
329 private void BaseType (Type t)
332 BaseTypeEventArgs ea = new BaseTypeEventArgs (t);
336 if (ReceiveBaseType != null)
337 ReceiveBaseType (this, ea);
342 protected virtual void OnBaseType (BaseTypeEventArgs e) {}
344 private void Interfaces (Type[] i)
346 if (ShowInterfaces) {
347 InterfacesEventArgs ea = new InterfacesEventArgs (i);
351 if (ReceiveInterfaces != null)
352 ReceiveInterfaces (this, ea);
357 protected virtual void OnInterfaces (InterfacesEventArgs e) {}
359 private void Fields (FieldInfo[] f)
362 FieldsEventArgs ea = new FieldsEventArgs (f);
366 if (ReceiveFields != null)
367 ReceiveFields (this, ea);
372 protected virtual void OnFields (FieldsEventArgs e) {}
374 private void Properties (PropertyInfo[] p)
376 if (ShowProperties) {
377 PropertiesEventArgs ea = new PropertiesEventArgs (p);
381 if (ReceiveProperties != null)
382 ReceiveProperties (this, ea);
387 protected virtual void OnProperties (PropertiesEventArgs e) {}
389 private void Events (EventInfo[] e)
392 EventsEventArgs ea = new EventsEventArgs (e);
396 if (ReceiveEvents != null)
397 ReceiveEvents (this, ea);
402 protected virtual void OnEvents (EventsEventArgs e) {}
404 private void Constructors (ConstructorInfo[] c)
406 if (ShowConstructors) {
407 ConstructorsEventArgs ea = new ConstructorsEventArgs (c);
411 if (ReceiveConstructors != null)
412 ReceiveConstructors (this, ea);
417 protected virtual void OnConstructors (ConstructorsEventArgs e)
421 private void Methods (MethodInfo[] m)
424 MethodsEventArgs ea = new MethodsEventArgs (m);
428 if (ReceiveMethods != null)
429 ReceiveMethods (this, ea);
434 protected virtual void OnMethods (MethodsEventArgs e) {}
436 protected static string GetEnumDescription (Type enumType, object value)
438 StringBuilder sb = new StringBuilder ();
439 sb.Append (Enum.Format(enumType, value, "f"));
442 sb.Append (String.Format ("0x{0}", Enum.Format (enumType, value, "x")));
445 sb.Append ("<unable to determine enumeration value>");
448 return sb.ToString ();
451 protected static string GetTypeKeyword (Type type)
457 else if (type.IsEnum)
459 else if (type.IsValueType)
461 else if (type.IsInterface)
469 protected static string GetTypeHeader (Type type)
471 return String.Format ("{0,-11}{1}", GetTypeKeyword (type), type.ToString());
474 public event TypeEventHandler ReceiveTypes;
475 public event BaseTypeEventHandler ReceiveBaseType;
476 public event InterfacesEventHandler ReceiveInterfaces;
477 public event FieldsEventHandler ReceiveFields;
478 public event PropertiesEventHandler ReceiveProperties;
479 public event EventsEventHandler ReceiveEvents;
480 public event ConstructorsEventHandler ReceiveConstructors;
481 public event MethodsEventHandler ReceiveMethods;