Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Created August 22, 2011 03:17
Show Gist options
  • Save peteraritchie/1161581 to your computer and use it in GitHub Desktop.
Save peteraritchie/1161581 to your computer and use it in GitHub Desktop.
code to create an Action<T> that will call a method that takes an object.
sbc.Subscribe(subs =>
{
Type type = GetRuntimeType(); // e.g. reflecting IHandle type...
//get pointer to generic method of specific type parameter
var methodInfo = subs.GetType().GetMethod("Handler", BindingFlags.Instance | BindingFlags.Public);
methodInfo = methodInfo.MakeGenericMethod(type);
// set up an action compatible with our method
// object is in commmon with any type we find above, so this will work
Action<object> a = m => serviceBus.Publish(m);
// following call to invoke is equivalent to:
// sbc.Handler<T>(m => serviceBus.Publish(m));
methodInfo.Invoke(subs, new object[] {a});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment