SharpLibUtils/Reflection.cs
author StephaneLenclud
Thu, 25 Aug 2016 13:34:05 +0200
changeset 255 c57b8ac80fc6
parent 213 77092f415c7c
child 260 d44943088c67
permissions -rw-r--r--
Published v1.0.2.0.
Fixed Harmony async issue prevent the config to be fetched.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Reflection;
     7 
     8 
     9 namespace SharpLib.Utils
    10 {
    11     public class Reflection
    12     {
    13         /// <summary>
    14         /// Get a list of all the concrete types derived from the given type in all loaded assembly.
    15         /// </summary>
    16         /// <typeparam name="T"></typeparam>
    17         /// <returns></returns>
    18         public static IEnumerable<Type> GetConcreteClassesDerivedFrom<T>() where T : class
    19         {
    20             List<Type> objects = new List<Type>();
    21 
    22             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    23             {
    24                 foreach (Type type in asm.GetTypes()
    25                         .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    26                 {
    27                     objects.Add(type);
    28                 }
    29             }
    30 
    31             return objects;
    32         }
    33 
    34         /// <summary>
    35         /// Get a dictionary of all the concrete types derived from the given type in all loaded assembly.
    36         /// </summary>
    37         /// <typeparam name="T"></typeparam>
    38         /// <returns></returns>
    39         public static IDictionary<string, Type> GetConcreteClassesDerivedFromByName<T>() where T : class
    40         {
    41             Dictionary<string, Type> objects = new Dictionary<string, Type>();
    42             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    43             {
    44                 foreach (Type type in asm.GetTypes()
    45                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    46                 {
    47                     objects.Add(type.Name, type);
    48                 }
    49             }
    50             return objects;
    51         }
    52 
    53         /// <summary>
    54         /// Get a list of an instance of all the types derived from the given type in all loaded assembly.
    55         /// </summary>
    56         /// <typeparam name="T"></typeparam>
    57         /// <returns></returns>
    58         public static IEnumerable<T> GetConcreteClassesInstanceDerivedFrom<T>() where T : class
    59         {
    60             List<T> objects = new List<T>();
    61             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    62             {
    63                 foreach (Type type in asm.GetTypes()
    64                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    65                 {
    66                     objects.Add((T)Activator.CreateInstance(type));
    67                 }
    68             }
    69 
    70             if (objects.Count>0
    71                 && objects[0] is IComparable)
    72             {
    73                 objects.Sort();
    74             }
    75 
    76             return objects;
    77         }
    78 
    79         /// <summary>
    80         /// Get a dictionary of an instance of all the types derived from the given type in all loaded assembly.
    81         /// </summary>
    82         /// <typeparam name="T"></typeparam>
    83         /// <returns></returns>
    84         public static IDictionary<string, T> GetConcreteClassesInstanceDerivedFromByName<T>() where T : class
    85         {
    86             Dictionary<string, T> objects = new Dictionary<string, T>();
    87             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    88             {
    89                 foreach (Type type in asm.GetTypes()
    90                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    91                 {
    92                     objects.Add(type.Name, (T)Activator.CreateInstance(type));
    93                 }
    94             }
    95 
    96 
    97             return objects;
    98         }
    99 
   100 
   101 
   102         /// <summary>
   103         /// Get the attribute instance matching the given attribute type from the specified type.
   104         /// </summary>
   105         /// <typeparam name="T"></typeparam>
   106         /// <param name="aType"></param>
   107         /// <returns></returns>
   108         public static T GetAttribute<T>(Type aType) where T : class
   109         {
   110             object[] attrs = aType.GetCustomAttributes(true);
   111             foreach (object attr in attrs)
   112             {
   113                 T attribute = attr as T;
   114                 if (attribute != null)
   115                 {
   116                     return attribute;
   117                 }
   118             }
   119 
   120             return null;
   121         }
   122 
   123         /// <summary>
   124         /// Get a list of all the types derived from the given type in all loaded assembly.
   125         /// </summary>
   126         /// <param name="baseType"></param>
   127         /// <param name="assembly"></param>
   128         /// <returns></returns>
   129         public static IEnumerable<Type> GetDerivedTypes<T>() where T: class
   130         {
   131             List<Type> types = new List<Type>();
   132             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
   133             {
   134                 foreach (Type type in asm.GetTypes()
   135                 .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(T))))
   136                 {
   137                     types.Add(type);
   138                 }
   139             }
   140             return types;
   141         }
   142     }
   143 }