Created
December 8, 2017 01:40
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Autofac.Util; | |
namespace MediatR.Pair | |
{ | |
public static class MediatorPair | |
{ | |
public static IEnumerable<Type> FindUnmatchedRequests(Assembly assembly) | |
{ | |
var requests = assembly.GetTypes() | |
.Where(t => t.IsClass && t.IsClosedTypeOf(typeof(IRequest<>))) | |
.ToList(); | |
var handlerInterfaces = assembly.GetTypes() | |
.Where(t => t.IsClass && (t.IsClosedTypeOf(typeof(IRequestHandler<>)) || t.IsClosedTypeOf(typeof(IRequestHandler<,>)))) | |
.SelectMany(t => t.GetInterfaces()) | |
.ToList(); | |
return (from request in requests | |
let resultType = request.GetInterfaces() | |
.Single(i => i.IsClosedTypeOf(typeof(IRequest<>)) && i.GetGenericArguments().Any()) | |
.GetGenericArguments() | |
.First() | |
let handlerType = resultType == typeof(Unit) | |
? typeof(IRequestHandler<>).MakeGenericType(request) | |
: typeof(IRequestHandler<,>).MakeGenericType(request, resultType) | |
where handlerInterfaces.Any(t => t == handlerType) == false | |
select request).ToList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment