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