Reflection functions now working on all loaded assemblies.
authorStephaneLenclud
Sun, 24 Jul 2016 14:22:56 +0200
changeset 21377092f415c7c
parent 212 1a0791daa243
child 214 4961ede27e0a
Reflection functions now working on all loaded assemblies.
SharpLibUtils/Reflection.cs
     1.1 --- a/SharpLibUtils/Reflection.cs	Sun Jul 24 13:30:08 2016 +0200
     1.2 +++ b/SharpLibUtils/Reflection.cs	Sun Jul 24 14:22:56 2016 +0200
     1.3 @@ -12,54 +12,60 @@
     1.4      public class Reflection
     1.5      {
     1.6          /// <summary>
     1.7 -        /// 
     1.8 +        /// Get a list of all the concrete types derived from the given type in all loaded assembly.
     1.9          /// </summary>
    1.10          /// <typeparam name="T"></typeparam>
    1.11          /// <returns></returns>
    1.12          public static IEnumerable<Type> GetConcreteClassesDerivedFrom<T>() where T : class
    1.13          {
    1.14              List<Type> objects = new List<Type>();
    1.15 -            foreach (Type type in
    1.16 -                Assembly.GetAssembly(typeof(T)).GetTypes()
    1.17 -                .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    1.18 +
    1.19 +            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    1.20              {
    1.21 -                objects.Add(type);
    1.22 +                foreach (Type type in asm.GetTypes()
    1.23 +                        .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    1.24 +                {
    1.25 +                    objects.Add(type);
    1.26 +                }
    1.27              }
    1.28  
    1.29              return objects;
    1.30          }
    1.31  
    1.32          /// <summary>
    1.33 -        /// 
    1.34 +        /// Get a dictionary of all the concrete types derived from the given type in all loaded assembly.
    1.35          /// </summary>
    1.36          /// <typeparam name="T"></typeparam>
    1.37          /// <returns></returns>
    1.38          public static IDictionary<string, Type> GetConcreteClassesDerivedFromByName<T>() where T : class
    1.39          {
    1.40              Dictionary<string, Type> objects = new Dictionary<string, Type>();
    1.41 -            foreach (Type type in
    1.42 -                Assembly.GetAssembly(typeof(T)).GetTypes()
    1.43 +            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    1.44 +            {
    1.45 +                foreach (Type type in asm.GetTypes()
    1.46                  .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    1.47 -            {
    1.48 -                objects.Add(type.Name,type);
    1.49 +                {
    1.50 +                    objects.Add(type.Name, type);
    1.51 +                }
    1.52              }
    1.53 -
    1.54              return objects;
    1.55          }
    1.56  
    1.57          /// <summary>
    1.58 -        /// 
    1.59 +        /// Get a list of an instance of all the types derived from the given type in all loaded assembly.
    1.60          /// </summary>
    1.61          /// <typeparam name="T"></typeparam>
    1.62          /// <returns></returns>
    1.63          public static IEnumerable<T> GetConcreteClassesInstanceDerivedFrom<T>() where T : class
    1.64          {
    1.65              List<T> objects = new List<T>();
    1.66 -            foreach (Type type in
    1.67 -                Assembly.GetAssembly(typeof(T)).GetTypes()
    1.68 +            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    1.69 +            {
    1.70 +                foreach (Type type in asm.GetTypes()
    1.71                  .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    1.72 -            {
    1.73 -                objects.Add((T)Activator.CreateInstance(type));
    1.74 +                {
    1.75 +                    objects.Add((T)Activator.CreateInstance(type));
    1.76 +                }
    1.77              }
    1.78  
    1.79              if (objects.Count>0
    1.80 @@ -72,18 +78,20 @@
    1.81          }
    1.82  
    1.83          /// <summary>
    1.84 -        /// 
    1.85 +        /// Get a dictionary of an instance of all the types derived from the given type in all loaded assembly.
    1.86          /// </summary>
    1.87          /// <typeparam name="T"></typeparam>
    1.88          /// <returns></returns>
    1.89          public static IDictionary<string, T> GetConcreteClassesInstanceDerivedFromByName<T>() where T : class
    1.90          {
    1.91              Dictionary<string, T> objects = new Dictionary<string, T>();
    1.92 -            foreach (Type type in
    1.93 -                Assembly.GetAssembly(typeof(T)).GetTypes()
    1.94 +            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    1.95 +            {
    1.96 +                foreach (Type type in asm.GetTypes()
    1.97                  .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    1.98 -            {
    1.99 -                objects.Add(type.Name,(T)Activator.CreateInstance(type));
   1.100 +                {
   1.101 +                    objects.Add(type.Name, (T)Activator.CreateInstance(type));
   1.102 +                }
   1.103              }
   1.104  
   1.105  
   1.106 @@ -93,7 +101,7 @@
   1.107  
   1.108  
   1.109          /// <summary>
   1.110 -        /// 
   1.111 +        /// Get the attribute instance matching the given attribute type from the specified type.
   1.112          /// </summary>
   1.113          /// <typeparam name="T"></typeparam>
   1.114          /// <param name="aType"></param>
   1.115 @@ -114,20 +122,23 @@
   1.116          }
   1.117  
   1.118          /// <summary>
   1.119 -        /// 
   1.120 +        /// Get a list of all the types derived from the given type in all loaded assembly.
   1.121          /// </summary>
   1.122          /// <param name="baseType"></param>
   1.123          /// <param name="assembly"></param>
   1.124          /// <returns></returns>
   1.125          public static IEnumerable<Type> GetDerivedTypes<T>() where T: class
   1.126 -        {            
   1.127 -            var types = from t in Assembly.GetAssembly(typeof(T)).GetTypes()
   1.128 -                        where t.IsSubclassOf(typeof(T))
   1.129 -                        select t;
   1.130 -
   1.131 +        {
   1.132 +            List<Type> types = new List<Type>();
   1.133 +            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
   1.134 +            {
   1.135 +                foreach (Type type in asm.GetTypes()
   1.136 +                .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(T))))
   1.137 +                {
   1.138 +                    types.Add(type);
   1.139 +                }
   1.140 +            }
   1.141              return types;
   1.142          }
   1.143 -
   1.144      }
   1.145 -
   1.146  }