-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from SergeyGulik/gettypes_may_fail
Assembly.GetTypes can throw ReflectionTypeLoadException
- Loading branch information
Showing
7 changed files
with
75 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters