Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Util.cs @ 1329

Last change on this file since 1329 was 1329, checked in by epitzer, 15 years ago

refactoring/resharping

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Reflection;
5
6namespace Persistence { 
7  public class InterfaceInstantiatior {
8    public static List<T> InstantiateAll<T>() {
9      return InstantiateAll<T>(Assembly.GetExecutingAssembly());
10    }
11    public static List<T> InstantiateAll<T>(Assembly a) {
12      List<T> instances = new List<T>();
13      foreach (Type t in a.GetTypes()) {
14        if (t.GetInterface(typeof(T).FullName) != null) {
15          ConstructorInfo ci = t.GetConstructor(new Type[] { });
16          if (ci != null) {
17            instances.Add((T)ci.Invoke(new object[] { }));
18          }
19        }
20      }
21      return instances;
22    }
23  }
24  public class Util {
25    public static string AutoFormat(object o) {
26      return AutoFormat(o, false);
27    }
28    public static string AutoFormat(object o, bool recursive) {
29      Dictionary<object, int> visitedObjects = new Dictionary<object,int>();
30      return AutoFormat(o, recursive, visitedObjects);
31    }
32    private static string AutoFormat(object o, bool recursive, IDictionary<object, int> visitedObjects) {
33      string s = o as string;
34      if (s != null)
35        return s;
36      if (o == null) {
37        return "<null>";
38      }
39      if (visitedObjects.ContainsKey(o)) {
40        return o.ToString();
41      }
42      visitedObjects.Add(o, 0);
43      if (o.ToString() != o.GetType().ToString()) {
44        return o.ToString();
45      }
46      StringBuilder sb = new StringBuilder();
47      sb.Append(o.GetType().Name);
48      sb.Append("(");
49      foreach (MemberInfo mInfo in o.GetType().GetMembers(
50        BindingFlags.Public |
51        BindingFlags.NonPublic |
52        BindingFlags.Instance)) {
53        if (mInfo.MemberType == MemberTypes.Field) {
54          FieldInfo fInfo = (FieldInfo)mInfo;         
55          sb.Append(mInfo.Name);
56          sb.Append("=");         
57          if (recursive) {
58            sb.Append(AutoFormat(fInfo.GetValue(o), true, visitedObjects));
59          } else {
60            sb.Append(fInfo.GetValue(o));
61          }
62          sb.Append(", ");
63        }
64      }
65      sb.Remove(sb.Length - 2, 2);
66      sb.Append(")");
67      return sb.ToString();
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.