private ISubscription ConnectConsumerInterface(Type consumerInterfaceType)
{
var method = consumerInterfaceType.GetMethod("Consume");
if (method == null)
{
throw new ChinchillaException(
"Could not get Consume method from consumer, did you try to subscribe to IConsumer, instead of IConsumer<T>?");
}
var messageType = method.GetParameters()
.First()
.ParameterType;
var actionType = typeof(Action<,>).MakeGenericType(messageType, typeof(IDeliveryContext));
var consumeAction = Delegate.CreateDelegate(actionType, consumer, method);
if (consumer is IConfigurableConsumer)
{
var configureMethod = typeof(IConfigurableConsumer)
.GetMethod(nameof(IConfigurableConsumer.ConfigureSubscription));
var configureAction = Delegate.CreateDelegate(
typeof(Action<ISubscriptionBuilder>),
consumer,
configureMethod);
var genericSubscribeMethod = subscribeMethodWithConfiguration.MakeGenericMethod(messageType);
return (ISubscription)genericSubscribeMethod.Invoke(bus, new object[] { consumeAction, configureAction });
}
else
{
var genericSubscribeMethod = subscribeMethod.MakeGenericMethod(messageType);
return (ISubscription)genericSubscribeMethod.Invoke(bus, new object[] { consumeAction });
}
}
}