Skip to content

Commit

Permalink
Merge pull request #6 from Tri125/master
Browse files Browse the repository at this point in the history
Fix Assembly GetTypes() throwing exceptions
  • Loading branch information
tinohager authored Mar 31, 2021
2 parents cb27cdd + db895f3 commit 23d016d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/Nager.Country.Translation/AssemblieExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Nager.Country.Translation
{
internal static class AssemblieExtensions
{
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}

try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Nager.Country.Translation/TranslationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TranslationProvider()

var interfaceType = typeof(ILanguageTranslation);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.SelectMany(s => s.GetLoadableTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass);

foreach (var type in types)
Expand Down

0 comments on commit 23d016d

Please sign in to comment.