Skip to content

Instantly share code, notes, and snippets.

@jeremiahredekop
Created June 28, 2012 21:04
Show Gist options
  • Save jeremiahredekop/3013888 to your computer and use it in GitHub Desktop.
Save jeremiahredekop/3013888 to your computer and use it in GitHub Desktop.
Get Message Handler For Message
public static class ContainerExtensions
{
public static Delegate GetMessageHandlerForMessage(this ILifetimeScope lifetimeScope, object message)
{
MethodInfo methodInfo = typeof(ContainerExtensions)
.GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
.Single(m => m.Name == "GetMessageHandlerForMessageInternal");
var methodInfo2 = methodInfo.MakeGenericMethod(new[] { message.GetType() });
return (Delegate)methodInfo2.Invoke(null, new[] { lifetimeScope, message });
}
private static Action<T> GetMessageHandlerForMessageInternal<T>(ILifetimeScope lifetimeScope, T message)
{
var commandHandler = lifetimeScope.Resolve<IHandleMessages<T>>();
return commandHandler.Handle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment