Skip to content

Commit

Permalink
add findAssemblies for discovering types
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-smit-ag committed Feb 27, 2024
1 parent 5077c60 commit bfd5ceb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Reflection;

namespace PiBox.Hosting.Abstractions.Extensions
{
public static class AssemblyExtensions
{
public static bool HasType(this Assembly assembly, Predicate<Type> typePredicate)
{
return assembly.GetTypes().Any(t => typePredicate(t));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using PiBox.Hosting.Abstractions.Services;

namespace PiBox.Hosting.Abstractions.Extensions
Expand All @@ -16,5 +17,7 @@ public static List<T> FindAndResolve<T>(this IImplementationResolver implementat
.Where(x => x is not null)
.OfType<T>().ToList();

public static List<Assembly> FindAssemblies(this IImplementationResolver implementationResolver,
Predicate<Assembly> filter) => implementationResolver.FindAssemblies().Where(f => filter(f)).ToList();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Reflection;

namespace PiBox.Hosting.Abstractions.Services
{
public interface IImplementationResolver
{
void ClearInstances();
object ResolveInstance(Type type);
List<Type> FindTypes();
List<Assembly> FindAssemblies();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public List<Type> FindTypes()
return _resolvedTypes.ToList();
}

public List<Assembly> FindAssemblies()
{
return _resolvedTypes.Select(x => x.Assembly).Distinct().ToList();
}

public void ClearInstances()
{
foreach (var disposable in _instances.OfType<IDisposable>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public void CanFindASpecificType()
types.Should().NotContain(typeof(BaseClass));
}

[Test]
public void CanFindAssembliesForPlugins()
{
var typeService = new TypeImplementationResolver(_configuration, _resolvedTypes, new Dictionary<Type, object>());
var assemblies = typeService.FindAssemblies();
assemblies.Should().HaveCount(1);
var assembly = assemblies.Single();
assembly.Should().BeSameAs(typeof(TypeImplementationResolverTests).Assembly);
assemblies = typeService.FindAssemblies(f => f.HasType(t => t.HasAttribute<ConfigurationAttribute>()));
assemblies.Should().HaveCount(1);
assembly = assemblies.Single();
assembly.Should().BeSameAs(typeof(TypeImplementationResolverTests).Assembly);
}

[Test]
public void InstancesWillBeReused()
{
Expand Down

0 comments on commit bfd5ceb

Please sign in to comment.