Skip to content

Instantly share code, notes, and snippets.

@jeremiahredekop
Created June 28, 2012 21:04
Show Gist options
  • Select an option

  • Save jeremiahredekop/3013888 to your computer and use it in GitHub Desktop.

Select an option

Save jeremiahredekop/3013888 to your computer and use it in GitHub Desktop.

Revisions

  1. jeremiahredekop created this gist Jun 28, 2012.
    19 changes: 19 additions & 0 deletions GetMessageHandlerForMessage.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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;
    }
    }