-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReflectionHelper.cs
121 lines (98 loc) · 4.21 KB
/
ReflectionHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Reflection;
namespace HtmlDocGenerator;
public static class ReflectionHelper
{
/// <summary>
/// An empty <see cref="object"/> array. Useful when calling parameterless constructors.
/// </summary>
public static readonly object[] EmptyObjectArray = new object[0];
/// <summary>Finds all types that derive from the provided class type.</summary>
/// <typeparam name="T">The base type of other classes to search for.</typeparam>
/// <returns></returns>
public static List<Type> FindType<T>(bool includeAbstract = false)
{
Type bType = typeof(T);
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
List<Type> result = new List<Type>();
foreach (Assembly assembly in assemblies)
{
IEnumerable<Type> types = assembly.DefinedTypes.Where(t => t.IsSubclassOf(bType) && (includeAbstract || !t.IsAbstract));
result.AddRange(types);
}
return result;
}
/// <summary>Finds all types that derive from the provided class type.</summary>
/// <typeparam name="T">The base type of other classes to search for.</typeparam>
/// <returns></returns>
public static IEnumerable<Type> FindTypeInParentAssembly<T>(bool includeAbstract = false)
{
Type bType = typeof(T);
return bType.Assembly.GetTypes().Where(t => t.IsSubclassOf(bType) && (includeAbstract || !t.IsAbstract));
}
public static IEnumerable<Type> FindType<T>(Assembly assembly, bool includeAbstract = false)
{
Type bType = typeof(T);
return assembly.GetTypes().Where(t =>
!t.IsGenericType && (t.IsSubclassOf(bType) && (includeAbstract || !t.IsAbstract) ||
(bType.IsAssignableFrom(t) && !t.IsInterface)));
}
public static IEnumerable<Type> FindTypesWithAttribute<T>(Assembly assembly, bool includeAbstract = false) where T : Attribute
{
Type bType = typeof(T);
return assembly.GetTypes().Where(t => t.GetCustomAttribute<T>() != null && (includeAbstract || !t.IsAbstract));
}
/// <summary>Gets the name of a type. Includes its namespace and name only.</summary>
/// <param name="type">The type of which to retrieve the name.</param>
/// <returns></returns>
public static string GetTypeName(Type type)
{
string typeName = "";
typeName = type.Namespace + "." + type.Name;
if (type.IsGenericType)
{
typeName += "<";
Type[] generics = type.GetGenericArguments();
for (int i = 0; i < generics.Length; i++)
{
if (i > 0)
typeName += ", " + GetTypeName(generics[i]);
else
typeName += GetTypeName(generics[i]);
}
typeName += ">";
}
return typeName;
}
/// <summary>Produces a short string that can be used to retrieve the type via Type.GetType.</summary>
/// <param name="type">The type to create the string for.</param>
/// <returns></returns>
public static string GetQualifiedTypeName(Type type)
{
string[] delims = new string[1] { ", " };
return type + ", " + type.Assembly.FullName.Split(delims, StringSplitOptions.RemoveEmptyEntries)[0];
}
/// <summary>Returns an array containing all of the values in the specified enum type.</summary>
/// <typeparam name="T">The enum type of which to return values.</typeparam>
/// <returns></returns>
public static T[] GetEnumValues<T>() where T : struct
{
Type t = typeof(T);
if (!t.IsEnum)
throw new Exception("The provided type was not of type Enum");
return Enum.GetValues(t) as T[];
}
public static T GetLastEnumValue<T>() where T : struct, IConvertible
{
Type t = typeof(T);
if (t.IsEnum == false)
throw new InvalidOperationException("Type must be an enum");
return Enum.GetValues(t).Cast<T>().Last();
}
public static T GetHighestEnumValue<T>() where T : struct, IConvertible
{
Type t = typeof(T);
if (t.IsEnum == false)
throw new InvalidOperationException("Type must be an enum");
return Enum.GetValues(t).Cast<T>().Max();
}
}