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
RevLine 
[1274]1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Reflection;
5
[1324]6namespace Persistence { 
[1318]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  }
[1274]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) {
[1279]29      Dictionary<object, int> visitedObjects = new Dictionary<object,int>();
30      return AutoFormat(o, recursive, visitedObjects);
31    }
[1329]32    private static string AutoFormat(object o, bool recursive, IDictionary<object, int> visitedObjects) {
[1274]33      string s = o as string;
34      if (s != null)
35        return s;
36      if (o == null) {
37        return "<null>";
38      }
[1279]39      if (visitedObjects.ContainsKey(o)) {
40        return o.ToString();
[1329]41      }
42      visitedObjects.Add(o, 0);
[1279]43      if (o.ToString() != o.GetType().ToString()) {
44        return o.ToString();
45      }
[1274]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) {
[1279]54          FieldInfo fInfo = (FieldInfo)mInfo;         
[1274]55          sb.Append(mInfo.Name);
[1279]56          sb.Append("=");         
[1274]57          if (recursive) {
[1329]58            sb.Append(AutoFormat(fInfo.GetValue(o), true, visitedObjects));
[1274]59          } else {
[1329]60            sb.Append(fInfo.GetValue(o));
[1274]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.