StephaneLenclud@210: using System;
StephaneLenclud@210: using System.Collections.Generic;
StephaneLenclud@210: using System.Linq;
StephaneLenclud@210: using System.Text;
StephaneLenclud@210: using System.Threading.Tasks;
StephaneLenclud@210: using System.Linq;
StephaneLenclud@210: using System.Reflection;
StephaneLenclud@210:
StephaneLenclud@210:
StephaneLenclud@210: namespace SharpLib.Utils
StephaneLenclud@210: {
StephaneLenclud@210: public class Reflection
StephaneLenclud@210: {
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: public static IEnumerable GetConcreteClassesDerivedFrom() where T : class
StephaneLenclud@210: {
StephaneLenclud@210: List objects = new List();
StephaneLenclud@210: foreach (Type type in
StephaneLenclud@210: Assembly.GetAssembly(typeof(T)).GetTypes()
StephaneLenclud@210: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
StephaneLenclud@210: {
StephaneLenclud@210: objects.Add(type);
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: return objects;
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: public static IDictionary GetConcreteClassesDerivedFromByName() where T : class
StephaneLenclud@210: {
StephaneLenclud@210: Dictionary objects = new Dictionary();
StephaneLenclud@210: foreach (Type type in
StephaneLenclud@210: Assembly.GetAssembly(typeof(T)).GetTypes()
StephaneLenclud@210: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
StephaneLenclud@210: {
StephaneLenclud@210: objects.Add(type.Name,type);
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: return objects;
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: public static IEnumerable GetConcreteClassesInstanceDerivedFrom() where T : class
StephaneLenclud@210: {
StephaneLenclud@210: List objects = new List();
StephaneLenclud@210: foreach (Type type in
StephaneLenclud@210: Assembly.GetAssembly(typeof(T)).GetTypes()
StephaneLenclud@210: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
StephaneLenclud@210: {
StephaneLenclud@210: objects.Add((T)Activator.CreateInstance(type));
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: if (objects.Count>0
StephaneLenclud@210: && objects[0] is IComparable)
StephaneLenclud@210: {
StephaneLenclud@210: objects.Sort();
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: return objects;
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: public static IDictionary GetConcreteClassesInstanceDerivedFromByName() where T : class
StephaneLenclud@210: {
StephaneLenclud@210: Dictionary objects = new Dictionary();
StephaneLenclud@210: foreach (Type type in
StephaneLenclud@210: Assembly.GetAssembly(typeof(T)).GetTypes()
StephaneLenclud@210: .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
StephaneLenclud@210: {
StephaneLenclud@210: objects.Add(type.Name,(T)Activator.CreateInstance(type));
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210:
StephaneLenclud@210: return objects;
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210:
StephaneLenclud@210:
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: ///
StephaneLenclud@210: public static T GetAttribute(Type aType) where T : class
StephaneLenclud@210: {
StephaneLenclud@210: object[] attrs = aType.GetCustomAttributes(true);
StephaneLenclud@210: foreach (object attr in attrs)
StephaneLenclud@210: {
StephaneLenclud@210: T attribute = attr as T;
StephaneLenclud@210: if (attribute != null)
StephaneLenclud@210: {
StephaneLenclud@210: return attribute;
StephaneLenclud@210: }
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: return null;
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: }
StephaneLenclud@210:
StephaneLenclud@210: }