Skip to content

Commit 840c1ed

Browse files
committed
Better and faster reflection type loading
1 parent 73d035f commit 840c1ed

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

Apex.Serialization/Apex.Serialization.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;netcoreapp3.0</TargetFrameworks>
55
<LangVersion>8.0</LangVersion>
6-
<Version>1.3.0</Version>
6+
<Version>1.3.1</Version>
77
<Authors>Dominic Bolin</Authors>
88
<Description>A high performance contract-less binary serializer</Description>
99
<PackageProjectUrl>https://github.com/dbolin/Apex.Serialization</PackageProjectUrl>

Apex.Serialization/Internal/Reflection/StaticTypeInfo.cs

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Linq;
65
using System.Reflection;
7-
using System.Runtime.InteropServices;
86

97
namespace Apex.Serialization.Internal.Reflection
108
{
@@ -29,35 +27,31 @@ internal static bool HasNoDescendents(Type t)
2927

3028
return _isSealedOrHasNoDescendantsMap.GetOrAdd(t, k =>
3129
{
32-
foreach (var assembly in AllAssemblies())
30+
if(t == typeof(object))
3331
{
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))
4538
{
39+
return false;
4640
}
4741
}
48-
4942
return true;
50-
});
43+
}
44+
);
5145
}
5246

53-
private static HashSet<Assembly>? _allAssemblies;
47+
private static HashSet<Type>? _allTypes;
5448
private static object _allAssembliesLock = new object();
5549

56-
private static IEnumerable<Assembly> AllAssemblies()
50+
private static IEnumerable<Type> AllTypes()
5751
{
58-
if (_allAssemblies != null)
52+
if (_allTypes != null)
5953
{
60-
return _allAssemblies;
54+
return _allTypes;
6155
}
6256

6357
lock (_allAssembliesLock)
@@ -71,9 +65,51 @@ private static IEnumerable<Assembly> AllAssemblies()
7165
allAssemblies.Add(assembly);
7266
}
7367

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+
}
76110
}
111+
112+
return result;
77113
}
78114

79115
private static void Add(HashSet<Assembly> allAssemblies, Assembly? initial)

0 commit comments

Comments
 (0)