SharpLibUtils/Reflection.cs
author StephaneLenclud
Sat, 23 Jul 2016 16:00:04 +0200
changeset 210 83dd86e73448
child 212 1a0791daa243
permissions -rw-r--r--
Most basic event action manager.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Linq;
     7 using System.Reflection;
     8 
     9 
    10 namespace SharpLib.Utils
    11 {
    12     public class Reflection
    13     {
    14         /// <summary>
    15         /// 
    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             foreach (Type type in
    23                 Assembly.GetAssembly(typeof(T)).GetTypes()
    24                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    25             {
    26                 objects.Add(type);
    27             }
    28 
    29             return objects;
    30         }
    31 
    32         /// <summary>
    33         /// 
    34         /// </summary>
    35         /// <typeparam name="T"></typeparam>
    36         /// <returns></returns>
    37         public static IDictionary<string, Type> GetConcreteClassesDerivedFromByName<T>() where T : class
    38         {
    39             Dictionary<string, Type> objects = new Dictionary<string, Type>();
    40             foreach (Type type in
    41                 Assembly.GetAssembly(typeof(T)).GetTypes()
    42                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    43             {
    44                 objects.Add(type.Name,type);
    45             }
    46 
    47             return objects;
    48         }
    49 
    50         /// <summary>
    51         /// 
    52         /// </summary>
    53         /// <typeparam name="T"></typeparam>
    54         /// <returns></returns>
    55         public static IEnumerable<T> GetConcreteClassesInstanceDerivedFrom<T>() where T : class
    56         {
    57             List<T> objects = new List<T>();
    58             foreach (Type type in
    59                 Assembly.GetAssembly(typeof(T)).GetTypes()
    60                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    61             {
    62                 objects.Add((T)Activator.CreateInstance(type));
    63             }
    64 
    65             if (objects.Count>0
    66                 && objects[0] is IComparable)
    67             {
    68                 objects.Sort();
    69             }
    70 
    71             return objects;
    72         }
    73 
    74         /// <summary>
    75         /// 
    76         /// </summary>
    77         /// <typeparam name="T"></typeparam>
    78         /// <returns></returns>
    79         public static IDictionary<string, T> GetConcreteClassesInstanceDerivedFromByName<T>() where T : class
    80         {
    81             Dictionary<string, T> objects = new Dictionary<string, T>();
    82             foreach (Type type in
    83                 Assembly.GetAssembly(typeof(T)).GetTypes()
    84                 .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
    85             {
    86                 objects.Add(type.Name,(T)Activator.CreateInstance(type));
    87             }
    88 
    89 
    90             return objects;
    91         }
    92 
    93 
    94 
    95         /// <summary>
    96         /// 
    97         /// </summary>
    98         /// <typeparam name="T"></typeparam>
    99         /// <param name="aType"></param>
   100         /// <returns></returns>
   101         public static T GetAttribute<T>(Type aType) where T : class
   102         {
   103             object[] attrs = aType.GetCustomAttributes(true);
   104             foreach (object attr in attrs)
   105             {
   106                 T attribute = attr as T;
   107                 if (attribute != null)
   108                 {
   109                     return attribute;
   110                 }
   111             }
   112 
   113             return null;
   114         }
   115 
   116     }
   117 
   118 }