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