StephaneLenclud@210: using System; StephaneLenclud@210: using System.Collections.Generic; StephaneLenclud@210: using System.Linq; StephaneLenclud@210: using System.Text; StephaneLenclud@210: using System.Threading.Tasks; StephaneLenclud@210: using System.Reflection; StephaneLenclud@210: StephaneLenclud@210: StephaneLenclud@210: namespace SharpLib.Utils StephaneLenclud@210: { StephaneLenclud@210: public class Reflection StephaneLenclud@210: { StephaneLenclud@210: /// StephaneLenclud@213: /// Get a list of all the concrete types derived from the given type in all loaded assembly. StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: public static IEnumerable GetConcreteClassesDerivedFrom() where T : class StephaneLenclud@210: { StephaneLenclud@210: List objects = new List(); StephaneLenclud@213: StephaneLenclud@213: foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) StephaneLenclud@210: { StephaneLenclud@213: foreach (Type type in asm.GetTypes() StephaneLenclud@213: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) StephaneLenclud@213: { StephaneLenclud@213: objects.Add(type); StephaneLenclud@213: } StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: return objects; StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: /// StephaneLenclud@213: /// Get a dictionary of all the concrete types derived from the given type in all loaded assembly. StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: public static IDictionary GetConcreteClassesDerivedFromByName() where T : class StephaneLenclud@210: { StephaneLenclud@210: Dictionary objects = new Dictionary(); StephaneLenclud@213: foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) StephaneLenclud@213: { StephaneLenclud@213: foreach (Type type in asm.GetTypes() StephaneLenclud@210: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) StephaneLenclud@213: { StephaneLenclud@213: objects.Add(type.Name, type); StephaneLenclud@213: } StephaneLenclud@210: } StephaneLenclud@210: return objects; StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: /// StephaneLenclud@213: /// Get a list of an instance of all the types derived from the given type in all loaded assembly. StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: public static IEnumerable GetConcreteClassesInstanceDerivedFrom() where T : class StephaneLenclud@210: { StephaneLenclud@210: List objects = new List(); StephaneLenclud@213: foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) StephaneLenclud@213: { StephaneLenclud@213: foreach (Type type in asm.GetTypes() StephaneLenclud@210: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) StephaneLenclud@213: { StephaneLenclud@213: objects.Add((T)Activator.CreateInstance(type)); StephaneLenclud@213: } StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: if (objects.Count>0 StephaneLenclud@210: && objects[0] is IComparable) StephaneLenclud@210: { StephaneLenclud@210: objects.Sort(); StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: return objects; StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: /// StephaneLenclud@213: /// Get a dictionary of an instance of all the types derived from the given type in all loaded assembly. StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: public static IDictionary GetConcreteClassesInstanceDerivedFromByName() where T : class StephaneLenclud@210: { StephaneLenclud@210: Dictionary objects = new Dictionary(); StephaneLenclud@213: foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) StephaneLenclud@213: { StephaneLenclud@213: foreach (Type type in asm.GetTypes() StephaneLenclud@210: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) StephaneLenclud@213: { StephaneLenclud@213: objects.Add(type.Name, (T)Activator.CreateInstance(type)); StephaneLenclud@213: } StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: StephaneLenclud@210: return objects; StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: StephaneLenclud@210: StephaneLenclud@210: /// StephaneLenclud@213: /// Get the attribute instance matching the given attribute type from the specified type. StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: /// StephaneLenclud@210: public static T GetAttribute(Type aType) where T : class StephaneLenclud@210: { StephaneLenclud@210: object[] attrs = aType.GetCustomAttributes(true); StephaneLenclud@210: foreach (object attr in attrs) StephaneLenclud@210: { StephaneLenclud@210: T attribute = attr as T; StephaneLenclud@210: if (attribute != null) StephaneLenclud@210: { StephaneLenclud@210: return attribute; StephaneLenclud@210: } StephaneLenclud@210: } StephaneLenclud@210: StephaneLenclud@210: return null; StephaneLenclud@210: } StephaneLenclud@210: Stephane@212: /// StephaneLenclud@213: /// Get a list of all the types derived from the given type in all loaded assembly. Stephane@212: /// Stephane@212: /// Stephane@212: /// Stephane@212: /// Stephane@212: public static IEnumerable GetDerivedTypes() where T: class StephaneLenclud@213: { StephaneLenclud@213: List types = new List(); StephaneLenclud@213: foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) StephaneLenclud@213: { StephaneLenclud@213: foreach (Type type in asm.GetTypes() StephaneLenclud@213: .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(T)))) StephaneLenclud@213: { StephaneLenclud@213: types.Add(type); StephaneLenclud@213: } StephaneLenclud@213: } Stephane@212: return types; Stephane@212: } StephaneLenclud@210: } StephaneLenclud@210: }