Skip to content

Instantly share code, notes, and snippets.

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