1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/SharpLibUtils/Reflection.cs Sat Jul 23 16:00:04 2016 +0200
1.3 @@ -0,0 +1,118 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Linq;
1.7 +using System.Text;
1.8 +using System.Threading.Tasks;
1.9 +using System.Linq;
1.10 +using System.Reflection;
1.11 +
1.12 +
1.13 +namespace SharpLib.Utils
1.14 +{
1.15 + public class Reflection
1.16 + {
1.17 + /// <summary>
1.18 + ///
1.19 + /// </summary>
1.20 + /// <typeparam name="T"></typeparam>
1.21 + /// <returns></returns>
1.22 + public static IEnumerable<Type> GetConcreteClassesDerivedFrom<T>() where T : class
1.23 + {
1.24 + List<Type> objects = new List<Type>();
1.25 + foreach (Type type in
1.26 + Assembly.GetAssembly(typeof(T)).GetTypes()
1.27 + .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
1.28 + {
1.29 + objects.Add(type);
1.30 + }
1.31 +
1.32 + return objects;
1.33 + }
1.34 +
1.35 + /// <summary>
1.36 + ///
1.37 + /// </summary>
1.38 + /// <typeparam name="T"></typeparam>
1.39 + /// <returns></returns>
1.40 + public static IDictionary<string, Type> GetConcreteClassesDerivedFromByName<T>() where T : class
1.41 + {
1.42 + Dictionary<string, Type> objects = new Dictionary<string, Type>();
1.43 + foreach (Type type in
1.44 + Assembly.GetAssembly(typeof(T)).GetTypes()
1.45 + .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
1.46 + {
1.47 + objects.Add(type.Name,type);
1.48 + }
1.49 +
1.50 + return objects;
1.51 + }
1.52 +
1.53 + /// <summary>
1.54 + ///
1.55 + /// </summary>
1.56 + /// <typeparam name="T"></typeparam>
1.57 + /// <returns></returns>
1.58 + public static IEnumerable<T> GetConcreteClassesInstanceDerivedFrom<T>() where T : class
1.59 + {
1.60 + List<T> objects = new List<T>();
1.61 + foreach (Type type in
1.62 + Assembly.GetAssembly(typeof(T)).GetTypes()
1.63 + .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
1.64 + {
1.65 + objects.Add((T)Activator.CreateInstance(type));
1.66 + }
1.67 +
1.68 + if (objects.Count>0
1.69 + && objects[0] is IComparable)
1.70 + {
1.71 + objects.Sort();
1.72 + }
1.73 +
1.74 + return objects;
1.75 + }
1.76 +
1.77 + /// <summary>
1.78 + ///
1.79 + /// </summary>
1.80 + /// <typeparam name="T"></typeparam>
1.81 + /// <returns></returns>
1.82 + public static IDictionary<string, T> GetConcreteClassesInstanceDerivedFromByName<T>() where T : class
1.83 + {
1.84 + Dictionary<string, T> objects = new Dictionary<string, T>();
1.85 + foreach (Type type in
1.86 + Assembly.GetAssembly(typeof(T)).GetTypes()
1.87 + .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
1.88 + {
1.89 + objects.Add(type.Name,(T)Activator.CreateInstance(type));
1.90 + }
1.91 +
1.92 +
1.93 + return objects;
1.94 + }
1.95 +
1.96 +
1.97 +
1.98 + /// <summary>
1.99 + ///
1.100 + /// </summary>
1.101 + /// <typeparam name="T"></typeparam>
1.102 + /// <param name="aType"></param>
1.103 + /// <returns></returns>
1.104 + public static T GetAttribute<T>(Type aType) where T : class
1.105 + {
1.106 + object[] attrs = aType.GetCustomAttributes(true);
1.107 + foreach (object attr in attrs)
1.108 + {
1.109 + T attribute = attr as T;
1.110 + if (attribute != null)
1.111 + {
1.112 + return attribute;
1.113 + }
1.114 + }
1.115 +
1.116 + return null;
1.117 + }
1.118 +
1.119 + }
1.120 +
1.121 +}