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