Skip to content

Instantly share code, notes, and snippets.

Created December 8, 2017 01:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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<>)))
var handlerInterfaces = assembly.GetTypes()
.Where(t => t.IsClass && (t.IsClosedTypeOf(typeof(IRequestHandler<>)) || t.IsClosedTypeOf(typeof(IRequestHandler<,>))))
.SelectMany(t => t.GetInterfaces())
return (from request in requests
let resultType = request.GetInterfaces()
.Single(i => i.IsClosedTypeOf(typeof(IRequest<>)) && i.GetGenericArguments().Any())
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