# HG changeset patch # User StephaneLenclud # Date 1469362976 -7200 # Node ID 77092f415c7c7163ef54f588bf1e4a2b89fd9bb3 # Parent 1a0791daa24310e659172b023c0175f68a63627e Reflection functions now working on all loaded assemblies. diff -r 1a0791daa243 -r 77092f415c7c SharpLibUtils/Reflection.cs --- a/SharpLibUtils/Reflection.cs Sun Jul 24 13:30:08 2016 +0200 +++ b/SharpLibUtils/Reflection.cs Sun Jul 24 14:22:56 2016 +0200 @@ -12,54 +12,60 @@ public class Reflection { /// - /// + /// Get a list of all the concrete types derived from the given type in all loaded assembly. /// /// /// public static IEnumerable GetConcreteClassesDerivedFrom() where T : class { List objects = new List(); - foreach (Type type in - Assembly.GetAssembly(typeof(T)).GetTypes() - .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) + + foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { - objects.Add(type); + foreach (Type type in asm.GetTypes() + .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) + { + objects.Add(type); + } } return objects; } /// - /// + /// Get a dictionary of all the concrete types derived from the given type in all loaded assembly. /// /// /// public static IDictionary GetConcreteClassesDerivedFromByName() where T : class { Dictionary objects = new Dictionary(); - foreach (Type type in - Assembly.GetAssembly(typeof(T)).GetTypes() + foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (Type type in asm.GetTypes() .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) - { - objects.Add(type.Name,type); + { + objects.Add(type.Name, type); + } } - return objects; } /// - /// + /// Get a list of an instance of all the types derived from the given type in all loaded assembly. /// /// /// public static IEnumerable GetConcreteClassesInstanceDerivedFrom() where T : class { List objects = new List(); - foreach (Type type in - Assembly.GetAssembly(typeof(T)).GetTypes() + foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (Type type in asm.GetTypes() .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) - { - objects.Add((T)Activator.CreateInstance(type)); + { + objects.Add((T)Activator.CreateInstance(type)); + } } if (objects.Count>0 @@ -72,18 +78,20 @@ } /// - /// + /// Get a dictionary of an instance of all the types derived from the given type in all loaded assembly. /// /// /// public static IDictionary GetConcreteClassesInstanceDerivedFromByName() where T : class { Dictionary objects = new Dictionary(); - foreach (Type type in - Assembly.GetAssembly(typeof(T)).GetTypes() + foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (Type type in asm.GetTypes() .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) - { - objects.Add(type.Name,(T)Activator.CreateInstance(type)); + { + objects.Add(type.Name, (T)Activator.CreateInstance(type)); + } } @@ -93,7 +101,7 @@ /// - /// + /// Get the attribute instance matching the given attribute type from the specified type. /// /// /// @@ -114,20 +122,23 @@ } /// - /// + /// Get a list of all the types derived from the given type in all loaded assembly. /// /// /// /// public static IEnumerable GetDerivedTypes() where T: class - { - var types = from t in Assembly.GetAssembly(typeof(T)).GetTypes() - where t.IsSubclassOf(typeof(T)) - select t; - + { + List types = new List(); + foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (Type type in asm.GetTypes() + .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(T)))) + { + types.Add(type); + } + } return types; } - } - }