Adding support for Display Messages.
2 using System.Collections.Generic;
5 using System.Threading.Tasks;
7 using System.Reflection;
10 namespace SharpLib.Utils
12 public class Reflection
15 /// Get a list of all the concrete types derived from the given type in all loaded assembly.
17 /// <typeparam name="T"></typeparam>
18 /// <returns></returns>
19 public static IEnumerable<Type> GetConcreteClassesDerivedFrom<T>() where T : class
21 List<Type> objects = new List<Type>();
23 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
25 foreach (Type type in asm.GetTypes()
26 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
36 /// Get a dictionary of all the concrete types derived from the given type in all loaded assembly.
38 /// <typeparam name="T"></typeparam>
39 /// <returns></returns>
40 public static IDictionary<string, Type> GetConcreteClassesDerivedFromByName<T>() where T : class
42 Dictionary<string, Type> objects = new Dictionary<string, Type>();
43 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
45 foreach (Type type in asm.GetTypes()
46 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
48 objects.Add(type.Name, type);
55 /// Get a list of an instance of all the types derived from the given type in all loaded assembly.
57 /// <typeparam name="T"></typeparam>
58 /// <returns></returns>
59 public static IEnumerable<T> GetConcreteClassesInstanceDerivedFrom<T>() where T : class
61 List<T> objects = new List<T>();
62 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
64 foreach (Type type in asm.GetTypes()
65 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
67 objects.Add((T)Activator.CreateInstance(type));
72 && objects[0] is IComparable)
81 /// Get a dictionary of an instance of all the types derived from the given type in all loaded assembly.
83 /// <typeparam name="T"></typeparam>
84 /// <returns></returns>
85 public static IDictionary<string, T> GetConcreteClassesInstanceDerivedFromByName<T>() where T : class
87 Dictionary<string, T> objects = new Dictionary<string, T>();
88 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
90 foreach (Type type in asm.GetTypes()
91 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
93 objects.Add(type.Name, (T)Activator.CreateInstance(type));
104 /// Get the attribute instance matching the given attribute type from the specified type.
106 /// <typeparam name="T"></typeparam>
107 /// <param name="aType"></param>
108 /// <returns></returns>
109 public static T GetAttribute<T>(Type aType) where T : class
111 object[] attrs = aType.GetCustomAttributes(true);
112 foreach (object attr in attrs)
114 T attribute = attr as T;
115 if (attribute != null)
125 /// Get a list of all the types derived from the given type in all loaded assembly.
127 /// <param name="baseType"></param>
128 /// <param name="assembly"></param>
129 /// <returns></returns>
130 public static IEnumerable<Type> GetDerivedTypes<T>() where T: class
132 List<Type> types = new List<Type>();
133 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
135 foreach (Type type in asm.GetTypes()
136 .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(T))))