Skip to content

Commit a407adb

Browse files
authored
fix: safely load assemblies in ConcertoTypeDictionary (#40)
* build: Build projects using .NET 8.0. - Update all projects' target framework in `.NET8.0` (from `standard2.0`) - Update all projects' C# version to 12 (from 10) - Updated NuGet packages - Fixes for compilation: 1. `ConcertoConverter.deserializeWithGenericType` chose the wrong overloaded method. Added expected parameters to choose the correct method. 2. Added `Decorator.cs` to declare the existence of `AccordProject.Concerto.Decorator` namespace, because the code generation script added `using AccordProject.Concerto.Decorator` for some reason and the compilation failed. Signed-off-by: Yuval Bavli <[email protected]> * fix: safely load assemblies Change assembly.GetTypes() to a more lenient approach "GetLoadableTypes()" where ReflectionTypeLoadException is being ignored, and we load only the types that we are able to load. The reason for this is that there may be some dlls that will fail to load which will fail the entire flow. For example the following error (which happened): ReflectionTypeLoadException : Unable to load one or more of the requested types.\nCould not load type 'SqlGuidCaster' from assembly 'System.Data.SqlClient, Version=4.6.1.6, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field." Signed-off-by: Yuval Bavli <[email protected]> --------- Signed-off-by: Yuval Bavli <[email protected]>
1 parent 69d25ea commit a407adb

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

AccordProject.Concerto/ConcertoTypeDictionary.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,28 @@ private void LoadTypesFromAssemblies(Assembly[] assemblies)
7979

8080
private void LoadTypesFromAssembly(Assembly assembly)
8181
{
82-
var types = assembly.GetTypes().Where(type =>
83-
type.IsClass
84-
&& typeof(Concept).IsAssignableFrom(type)
85-
&& type.GetCustomAttribute<TypeAttribute>() != null);
82+
var types = GetLoadableTypes(assembly).Where(
83+
type =>
84+
type.IsClass
85+
&& typeof(Concept).IsAssignableFrom(type)
86+
&& type.GetCustomAttribute<TypeAttribute>() != null);
8687
foreach (var type in types)
8788
{
8889
var attribute = type.GetCustomAttribute<TypeAttribute>();
8990
var key = attribute!.ToType();
9091
entries.Add(key, type);
9192
}
9293
}
94+
95+
public IEnumerable<Type> GetLoadableTypes(Assembly assembly)
96+
{
97+
try
98+
{
99+
return assembly.GetTypes();
100+
}
101+
catch (ReflectionTypeLoadException e)
102+
{
103+
return e.Types.Where(t => t != null)!;
104+
}
105+
}
93106
}

0 commit comments

Comments
 (0)