Skip to content

Commit

Permalink
Continue in the case not all types can be loaded from an asembly
Browse files Browse the repository at this point in the history
  • Loading branch information
FEIWORLD\mdejong authored and FEIWORLD\mdejong committed Oct 30, 2018
1 parent 75b7589 commit f898045
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/examples/TypesVisualizer/Reflection/TypesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@

namespace OpenSoftware.DgmlTools.Reflection
{
public static class AssemblyExtensions{
public static IEnumerable<Type> TryGetTypes(this Assembly a)
{
try
{
return a.GetTypes();
}
catch
{
Console.Error.WriteLine("Loading failed for " + a.FullName);
return new Type[]{};
}
}
}
public class TypesLoader : IDisposable
{
private readonly IEnumerable<string> _assemblyPaths;
Expand All @@ -22,6 +36,7 @@ public TypesLoader(ICollection<string> assemblyPaths, ICollection<IExcludeFilter
_excludeFilters = excludeFilters ?? new List<IExcludeFilter>();
_assemblySearchPaths = assemblyPaths.Select(Path.GetDirectoryName);
}

/// <summary>
/// Load types from specified collection of assemblies (and all type dependencies) excluding the types that
/// match any of the exclude filters.
Expand All @@ -32,12 +47,12 @@ public IEnumerable<Type> Load()
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += CurrentDomainOnReflectionOnlyAssemblyResolve;
var assemblies = _assemblyPaths.Select(Assembly.ReflectionOnlyLoadFrom);
assemblies =
assemblies.SelectMany(x => x.GetReferencedAssemblies().Select(LoadAssembly).Concat(new[] {x}));
assemblies.SelectMany(x => x.GetReferencedAssemblies().Select(LoadAssembly).Concat(new[] { x }));
var types = assemblies
.SelectMany(a => a.GetTypes()
.Where(x => x.IsClass || x.IsInterface)
.SelectMany(a => a.TryGetTypes()
.Where(x => x.IsClass || x.IsInterface)
.Where(new ExcludeFilters(_excludeFilters).Apply));
return types.OrderBy(x=>x.Name).ToArray();
return types.OrderBy(x => x.Name).ToArray();
}

private Assembly LoadAssembly(AssemblyName assemblyName)
Expand Down

0 comments on commit f898045

Please sign in to comment.