2
2
using System . Collections . Concurrent ;
3
3
using System . Collections . Generic ;
4
4
using System . IO ;
5
- using System . Linq ;
6
5
using System . Reflection ;
7
- using System . Runtime . InteropServices ;
8
6
9
7
namespace Apex . Serialization . Internal . Reflection
10
8
{
@@ -29,35 +27,31 @@ internal static bool HasNoDescendents(Type t)
29
27
30
28
return _isSealedOrHasNoDescendantsMap . GetOrAdd ( t , k =>
31
29
{
32
- foreach ( var assembly in AllAssemblies ( ) )
30
+ if ( t == typeof ( object ) )
33
31
{
34
- try
35
- {
36
- foreach ( var type in assembly . DefinedTypes )
37
- {
38
- if ( type . IsSubclassOf ( t ) )
39
- {
40
- return false ;
41
- }
42
- }
43
- }
44
- catch ( ReflectionTypeLoadException )
32
+ return false ;
33
+ }
34
+
35
+ foreach ( var type in AllTypes ( ) )
36
+ {
37
+ if ( type . IsSubclassOf ( t ) )
45
38
{
39
+ return false ;
46
40
}
47
41
}
48
-
49
42
return true ;
50
- } ) ;
43
+ }
44
+ ) ;
51
45
}
52
46
53
- private static HashSet < Assembly > ? _allAssemblies ;
47
+ private static HashSet < Type > ? _allTypes ;
54
48
private static object _allAssembliesLock = new object ( ) ;
55
49
56
- private static IEnumerable < Assembly > AllAssemblies ( )
50
+ private static IEnumerable < Type > AllTypes ( )
57
51
{
58
- if ( _allAssemblies != null )
52
+ if ( _allTypes != null )
59
53
{
60
- return _allAssemblies ;
54
+ return _allTypes ;
61
55
}
62
56
63
57
lock ( _allAssembliesLock )
@@ -71,9 +65,51 @@ private static IEnumerable<Assembly> AllAssemblies()
71
65
allAssemblies . Add ( assembly ) ;
72
66
}
73
67
74
- _allAssemblies = allAssemblies ;
75
- return allAssemblies ;
68
+ _allTypes = GetAllTypesFrom ( allAssemblies ) ;
69
+ return _allTypes ;
70
+ }
71
+ }
72
+
73
+ private static HashSet < Type > GetAllTypesFrom ( HashSet < Assembly > allAssemblies )
74
+ {
75
+ var result = new HashSet < Type > ( ) ;
76
+ foreach ( var assembly in allAssemblies )
77
+ {
78
+ try
79
+ {
80
+ foreach ( var type in assembly . DefinedTypes )
81
+ {
82
+ if ( type . BaseType == typeof ( object ) )
83
+ {
84
+ continue ;
85
+ }
86
+
87
+ result . Add ( type ) ;
88
+ }
89
+ }
90
+ catch ( ReflectionTypeLoadException e )
91
+ {
92
+ if ( e . Types != null )
93
+ {
94
+ foreach ( var type in e . Types )
95
+ {
96
+ if ( type == null )
97
+ {
98
+ continue ;
99
+ }
100
+
101
+ if ( type . BaseType == typeof ( object ) )
102
+ {
103
+ continue ;
104
+ }
105
+
106
+ result . Add ( type ) ;
107
+ }
108
+ }
109
+ }
76
110
}
111
+
112
+ return result ;
77
113
}
78
114
79
115
private static void Add ( HashSet < Assembly > allAssemblies , Assembly ? initial )
0 commit comments