forked from DanielAbalde/Tenrec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeUtils.cs
118 lines (113 loc) · 3.98 KB
/
TypeUtils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Tenrec.Components;
namespace Tenrec
{
public static class TypeUtils
{
public static string GetAssemblyDirectory(Assembly assembly = null)
{
if (assembly == null)
assembly = Assembly.GetExecutingAssembly();
UriBuilder uri = new UriBuilder(assembly.CodeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
public static IEnumerable<Type> GetTypesFromAssembly(Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e2)
{
return e2.Types.Where(t => t != null);
}
}
public static IEnumerable<Type> GetTypesFromAssemblyAssignableFrom(Assembly assembly, Type type, bool ignoreAbstracts = true, bool ignoreGenerics = false, bool ignorePrivates = true)
{
foreach (var t in GetTypesFromAssembly(assembly))
{
if (t == type)
continue;
if (ignoreAbstracts && t.IsAbstract)
continue;
if (ignoreGenerics && t.IsGenericType)
continue;
if (ignorePrivates && !t.IsPublic)
continue;
if (!IsType(t, type))
continue;
yield return t;
}
}
public static bool IsType(Type toCheck, Type type)
{
if (toCheck == null || type == null)
return false;
if (toCheck == type)
return true;
if (type.IsInterface)
{
if (type.IsGenericType)
return toCheck.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == type);
else
return type.IsAssignableFrom(toCheck);
}
else if (type.IsGenericType)
{
while (toCheck != null)
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (cur == type)
return true;
toCheck = toCheck.BaseType;
}
}
else
{
while (toCheck != null)
{
if (toCheck == type)
return true;
toCheck = toCheck.BaseType;
}
}
return false;
}
public static IEnumerable<Assembly> LoadTenrecAssembliesFrom(string directory = "")
{
if (string.IsNullOrEmpty(directory))
directory = GetAssemblyDirectory();// AppDomain.CurrentDomain.BaseDirectory;
else
if (!System.IO.Directory.Exists(directory))
throw new DirectoryNotFoundException(directory);
var assName = typeof(Group_UnitTest).Assembly.GetName().Name;
foreach (var ass in Directory.GetFiles(directory, "*.dll")
.Concat(Directory.GetFiles(directory, "*.gha")))
{
Assembly a = null;
try
{
var n = AssemblyName.GetAssemblyName(ass);
if (n == null)
continue;
a = Assembly.Load(n);
if (!(a.GetName().Name.Equals(assName) ||
a.GetReferencedAssemblies().Any(ra => ra.Name.Equals(assName))
&& !Guid.TryParse(a.GetName().Name, out _)))
continue;
}
catch
{
}
if (a == null)
continue;
yield return a;
}
}
}
}