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