Skip to content

Commit

Permalink
Merge pull request #24 from SergeyGulik/gettypes_may_fail
Browse files Browse the repository at this point in the history
Assembly.GetTypes can throw ReflectionTypeLoadException
  • Loading branch information
PatrickRitchie authored Apr 26, 2022
2 parents 3b2bcf3 + f361851 commit 700e7b1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
50 changes: 50 additions & 0 deletions src/MTConnect.NET-Common/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace MTConnect
{
public static class AssemblyExtensions
{
/// <summary>
/// Get the types within the assembly that match the predicate.
/// <para>for example, to get all types within a namespace</para>
/// <para> typeof(SomeClassInAssemblyYouWant).Assembly.GetMatchingTypesInAssembly(item => "MyNamespace".Equals(item.Namespace))</para>
/// </summary>
/// <param name="assembly">The assembly to search</param>
/// <param name="predicate">The predicate query to match against</param>
/// <returns>The collection of types within the assembly that match the predicate</returns>
/// <remarks>
/// Code taken from https://stackoverflow.com/questions/7889228/how-to-prevent-reflectiontypeloadexception-when-calling-assembly-gettypes
/// </remarks>
public static IReadOnlyCollection<Type> GetMatchingTypesInAssembly(
this Assembly assembly,
Predicate<Type> predicate)
{
var types = new List<Type>();
try
{
types = assembly.GetTypes().Where(i => predicate(i) && i.Assembly == assembly).ToList();
}
catch (ReflectionTypeLoadException ex)
{
foreach (var theType in ex.Types)
{
try
{
if (theType != null && predicate(theType) && theType.Assembly == assembly)
types.Add(theType);
}
// This exception list is not exhaustive, modify to suit any reasons
// you find for failure to parse a single assembly
catch (BadImageFormatException)
{
// Type not in this assembly - reference to elsewhere ignored
}
}
}
return types;
}
}
}
6 changes: 4 additions & 2 deletions src/MTConnect.NET-Common/Assets/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ private static Dictionary<string, Type> GetAllTypes()
var assemblies = Assemblies.Get();
if (!assemblies.IsNullOrEmpty())
{
var allTypes = assemblies.SelectMany(x => x.GetTypes());
var types = assemblies
.SelectMany(
x => x.GetMatchingTypesInAssembly(
t => typeof(IAsset).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract));

var types = allTypes.Where(x => typeof(IAsset).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
if (!types.IsNullOrEmpty())
{
var objs = new Dictionary<string, Type>();
Expand Down
6 changes: 4 additions & 2 deletions src/MTConnect.NET-Common/Devices/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ private static Dictionary<string, Type> GetAllTypes()
var assemblies = Assemblies.Get();
if (!assemblies.IsNullOrEmpty())
{
var allTypes = assemblies.SelectMany(x => x.GetTypes());
var types = assemblies
.SelectMany(
x => x.GetMatchingTypesInAssembly(
t => typeof(Component).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract));

var types = allTypes.Where(x => typeof(Component).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
if (!types.IsNullOrEmpty())
{
var objs = new Dictionary<string, Type>();
Expand Down
6 changes: 4 additions & 2 deletions src/MTConnect.NET-Common/Devices/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ private static Dictionary<string, Type> GetAllTypes()
var assemblies = Assemblies.Get();
if (!assemblies.IsNullOrEmpty())
{
var allTypes = assemblies.SelectMany(x => x.GetTypes());
var types = assemblies
.SelectMany(
x => x.GetMatchingTypesInAssembly(
t => typeof(Composition).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract));

var types = allTypes.Where(x => typeof(Composition).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
if (!types.IsNullOrEmpty())
{
var objs = new Dictionary<string, Type>();
Expand Down
6 changes: 4 additions & 2 deletions src/MTConnect.NET-Common/Devices/DataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ private static Dictionary<string, Type> GetAllTypes()
var assemblies = Assemblies.Get();
if (!assemblies.IsNullOrEmpty())
{
var allTypes = assemblies.SelectMany(x => x.GetTypes());
var types = assemblies
.SelectMany(
x => x.GetMatchingTypesInAssembly(
t => typeof(DataItem).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract));

var types = allTypes.Where(x => typeof(DataItem).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
if (!types.IsNullOrEmpty())
{
var objs = new Dictionary<string, Type>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,12 @@ private static void AddFormatters()
var assemblies = Assemblies.Get();
if (!assemblies.IsNullOrEmpty())
{
var allTypes = assemblies.SelectMany(x => x.GetTypes());

// Get IResponseDocumentFormatter Types
var types = allTypes.Where(x => typeof(IResponseDocumentFormatter).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
var types = assemblies
.SelectMany(
x => x.GetMatchingTypesInAssembly(
t => typeof(IResponseDocumentFormatter).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract));

if (!types.IsNullOrEmpty())
{
foreach (var type in types)
Expand Down
6 changes: 4 additions & 2 deletions src/MTConnect.NET-Common/Observations/Observation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,11 @@ protected static Dictionary<string, Type> GetAllTypes()
var assemblies = Assemblies.Get();
if (!assemblies.IsNullOrEmpty())
{
var allTypes = assemblies.SelectMany(x => x.GetTypes());
var types = assemblies
.SelectMany(
x => x.GetMatchingTypesInAssembly(
t => typeof(IObservation).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract));

var types = allTypes.Where(x => typeof(IObservation).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
if (!types.IsNullOrEmpty())
{
var objs = new Dictionary<string, Type>();
Expand Down

0 comments on commit 700e7b1

Please sign in to comment.