Created
December 8, 2017 01:40
-
-
Save dcomartin/04d0b7f1985e0766e69a189c3f9896dd to your computer and use it in GitHub Desktop.
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