Published v1.0.2.0.
Fixed Harmony async issue prevent the config to be fetched.
2 using System.Collections.Generic;
5 using System.Threading.Tasks;
6 using System.Reflection;
9 namespace SharpLib.Utils
11 public class Reflection
14 /// Get a list of all the concrete types derived from the given type in all loaded assembly.
16 /// <typeparam name="T"></typeparam>
17 /// <returns></returns>
18 public static IEnumerable<Type> GetConcreteClassesDerivedFrom<T>() where T : class
20 List<Type> objects = new List<Type>();
22 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
24 foreach (Type type in asm.GetTypes()
25 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
35 /// Get a dictionary of all the concrete types derived from the given type in all loaded assembly.
37 /// <typeparam name="T"></typeparam>
38 /// <returns></returns>
39 public static IDictionary<string, Type> GetConcreteClassesDerivedFromByName<T>() where T : class
41 Dictionary<string, Type> objects = new Dictionary<string, Type>();
42 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
44 foreach (Type type in asm.GetTypes()
45 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
47 objects.Add(type.Name, type);
54 /// Get a list of an instance of all the types derived from the given type in all loaded assembly.
56 /// <typeparam name="T"></typeparam>
57 /// <returns></returns>
58 public static IEnumerable<T> GetConcreteClassesInstanceDerivedFrom<T>() where T : class
60 List<T> objects = new List<T>();
61 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
63 foreach (Type type in asm.GetTypes()
64 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
66 objects.Add((T)Activator.CreateInstance(type));
71 && objects[0] is IComparable)
80 /// Get a dictionary of an instance of all the types derived from the given type in all loaded assembly.
82 /// <typeparam name="T"></typeparam>
83 /// <returns></returns>
84 public static IDictionary<string, T> GetConcreteClassesInstanceDerivedFromByName<T>() where T : class
86 Dictionary<string, T> objects = new Dictionary<string, T>();
87 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
89 foreach (Type type in asm.GetTypes()
90 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
92 objects.Add(type.Name, (T)Activator.CreateInstance(type));
103 /// Get the attribute instance matching the given attribute type from the specified type.
105 /// <typeparam name="T"></typeparam>
106 /// <param name="aType"></param>
107 /// <returns></returns>
108 public static T GetAttribute<T>(Type aType) where T : class
110 object[] attrs = aType.GetCustomAttributes(true);
111 foreach (object attr in attrs)
113 T attribute = attr as T;
114 if (attribute != null)
124 /// Get a list of all the types derived from the given type in all loaded assembly.
126 /// <param name="baseType"></param>
127 /// <param name="assembly"></param>
128 /// <returns></returns>
129 public static IEnumerable<Type> GetDerivedTypes<T>() where T: class
131 List<Type> types = new List<Type>();
132 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
134 foreach (Type type in asm.GetTypes()
135 .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(T))))