SharpLibUtils/Reflection.cs
author StephaneLenclud
Wed, 04 Jan 2017 18:43:28 +0100
changeset 274 920fea7a6427
parent 232 5a739e2e5255
permissions -rw-r--r--
Proper basic support for Audio Visualizer.
     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         /// That includes the given type itself if it's intanciable.
    16         /// </summary>
    17         /// <typeparam name="T"></typeparam>
    18         /// <returns></returns>
    19         public static IEnumerable<Type> GetConcreteClassesDerivedFrom<T>() where T : class
    20         {
    21             List<Type> objects = new List<Type>();
    22 
    23             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    24             {
    25                 foreach (Type type in asm.GetTypes()                        
    26                         .Where(myType => myType.IsClass && !myType.IsAbstract && (myType.IsSubclassOf(typeof(T)) || myType == typeof(T))))
    27                 {
    28                     objects.Add(type);
    29                 }
    30             }
    31 
    32             return objects;
    33         }
    34 
    35         /// <summary>
    36         /// Get a dictionary of all the concrete types derived from the given type in all loaded assembly.
    37         /// </summary>
    38         /// <typeparam name="T"></typeparam>
    39         /// <returns></returns>
    40         public static IDictionary<string, Type> GetConcreteClassesDerivedFromByName<T>() where T : class
    41         {
    42             Dictionary<string, Type> objects = new Dictionary<string, Type>();
    43             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    44             {
    45                 foreach (Type type in asm.GetTypes()
    46                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    47                 {
    48                     objects.Add(type.Name, type);
    49                 }
    50             }
    51             return objects;
    52         }
    53 
    54         /// <summary>
    55         /// Get a list of an instance of all the types derived from the given type in all loaded assembly.
    56         /// </summary>
    57         /// <typeparam name="T"></typeparam>
    58         /// <returns></returns>
    59         public static IEnumerable<T> GetConcreteClassesInstanceDerivedFrom<T>() where T : class
    60         {
    61             List<T> objects = new List<T>();
    62             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    63             {
    64                 foreach (Type type in asm.GetTypes()
    65                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    66                 {
    67                     objects.Add((T)Activator.CreateInstance(type));
    68                 }
    69             }
    70 
    71             if (objects.Count>0
    72                 && objects[0] is IComparable)
    73             {
    74                 objects.Sort();
    75             }
    76 
    77             return objects;
    78         }
    79 
    80         /// <summary>
    81         /// Get a dictionary of an instance of all the types derived from the given type in all loaded assembly.
    82         /// </summary>
    83         /// <typeparam name="T"></typeparam>
    84         /// <returns></returns>
    85         public static IDictionary<string, T> GetConcreteClassesInstanceDerivedFromByName<T>() where T : class
    86         {
    87             Dictionary<string, T> objects = new Dictionary<string, T>();
    88             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    89             {
    90                 foreach (Type type in asm.GetTypes()
    91                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    92                 {
    93                     objects.Add(type.Name, (T)Activator.CreateInstance(type));
    94                 }
    95             }
    96 
    97 
    98             return objects;
    99         }
   100 
   101 
   102 
   103         /// <summary>
   104         /// Get the attribute instance matching the given attribute type from the specified type.
   105         /// </summary>
   106         /// <typeparam name="T"></typeparam>
   107         /// <param name="aType"></param>
   108         /// <returns></returns>
   109         public static T GetAttribute<T>(Type aType) where T : class
   110         {
   111             object[] attrs = aType.GetCustomAttributes(true);
   112             foreach (object attr in attrs)
   113             {
   114                 T attribute = attr as T;
   115                 if (attribute != null)
   116                 {
   117                     return attribute;
   118                 }
   119             }
   120 
   121             return null;
   122         }
   123 
   124         /// <summary>
   125         /// Get a list of all the types derived from the given type in all loaded assembly.
   126         /// </summary>
   127         /// <param name="baseType"></param>
   128         /// <param name="assembly"></param>
   129         /// <returns></returns>
   130         public static IEnumerable<Type> GetDerivedTypes<T>() where T: class
   131         {
   132             List<Type> types = new List<Type>();
   133             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
   134             {
   135                 foreach (Type type in asm.GetTypes()
   136                 .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(T))))
   137                 {
   138                     types.Add(type);
   139                 }
   140             }
   141             return types;
   142         }
   143     }
   144 }