xplat
xplat

Reputation: 8636

Get all interface types in all assemblies referenced in project

I have an ASP.NET Core 2.1 application referencing in a solution couple of .NET Standard 2.0 projects. I want to use reflection and get all types and filter with specific interface implemented but it only returns the generic interfaces so the IsHandlerInterface method never return true.

List<AssemblyName> allAssemblies = Assembly.GetExecutingAssembly()
                                                        .GetReferencedAssemblies()
                                                        .Where(p => p.FullName.StartsWith("Something"))
                                                        .ToList(); // I get 4 assemblies here with the correct results

List<Type> allAssembliesTypes = allAssemblies
                                      .Select(a => a.GetType())
                                      .ToList(); // Retrieving the types

List<Type> handlerTypes = allAssembliesTypes
                                            // typeof(ICommand).Assembly.GetTypes()
                                            .Where(x => x.GetInterfaces().Any(y => IsHandlerInterface(y))) // Here I don't see the handler interface, only the generic ones, see method below
                                            .Where(x => x.Name.EndsWith("Handler")) // Maybe redundant
                                            .ToList();

private static bool IsHandlerInterface(Type type)
        {
            if (!type.IsGenericType)
                return false;

            Type typeDefinition = type.GetGenericTypeDefinition();

            return typeDefinition == typeof(ICommandHandler<>) || typeDefinition == typeof(IQueryHandler<,>);
        }

A handler example below.

public sealed class SampleCommandHandler : ICommandHandler<SampleCommand>
    {
        public SampleCommandHandler() // inject services
        {
        }

        public Task<Result> HandleAsync(SampleCommand command)
        {
            // command logic
            // preconditions handle
            // trigger events
            throw new NotImplementedException();
        }
    }

Upvotes: 2

Views: 2467

Answers (3)

Paul Aicher
Paul Aicher

Reputation: 261

AppDomain.CurrentDomain.GetAssemblies() //Gets all the assemblies loaded
    .SelectMany(x => x.GetTypes()) //Gets all the types in each assembly
    .Where(t => typeof(IsHandlerInterface).IsAssignableFrom(t)) //filters out any type that can not be assigned IsHandlerInterface
    .ToList();//Creates a list of all types with the interface IsHandlerInterface

Upvotes: 0

xplat
xplat

Reputation: 8636

I was able to get all assemblies with the referenced using with the below code.

List<Assembly> all = Assembly.GetEntryAssembly()
                             .GetReferencedAssemblies()
                             .Select(Assembly.Load);

Upvotes: 1

Hadi
Hadi

Reputation: 443

This code return all types that imelement ICommandHandler<> and IQueryHandler<>

var types = Assembly
    .GetEntryAssembly()
    .GetReferencedAssemblies()
    .Select(s => s.GetType())
    .Where(p => typeof(ICommandHandler<>).IsAssignableFrom(p) || typeof(IQueryHandler<>).IsAssignableFrom(p));

Upvotes: 1

Related Questions