如何获取下面代码 TestIntegrationEventHandler 所实现的接口 IIntegrationEventHandler 中的 TestIntegrationEvent 类型
public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent>
{ }
需要在下面的扩展方法中获取
public static void SubscribeByEventHandler<TEventHandler>(this IEndpointRouteBuilder builder)
where TEventHandler : IIntegrationEventHandler
{ }
调用扩展方法的代码
app.SubscribeByEventHandler<TestIntegrationEventHandler>();
通过下面的代码实现了
public static void SubscribeByEventHandler<TEventHandler>(this IEndpointRouteBuilder builder)
where TEventHandler : IIntegrationEventHandler
{
var expectedHandler = typeof(TEventHandler).GetInterfaces()
.Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IIntegrationEventHandler<>))
.FirstOrDefault();
var eventType = expectedHandler?.GetGenericArguments().FirstOrDefault();
Console.WriteLine(eventType);
}