1
比方说,我有这个类:注册泛型类型
public class DetailedQueryHandler<T> : IQueryHandlerAsync<Detailed, T>
where T : CalculationQuery
我可以这样注册它:
builder.RegisterType(typeof(DetailedQueryHandler<CalculationWithDealerQuery>))
.As(typeof(IQueryHandlerAsync<Detailed, CalculationWithDealerQuery>));
builder.RegisterType(typeof(DetailedQueryHandler<CalculationQuery>))
.As(typeof(IQueryHandlerAsync<Detailed, CalculationQuery>));
但我想更自动化的方式来注册它,就像我可以注册类型下面IQueryHandlerAsync接口:
var types = ThisAssembly.GetTypes();
builder.RegisterTypes(types)
.Where(t => t.ImplementGenericInterface(t2 => t2 == typeof(IQueryHandlerAsync<,>))
.AsImplementedInterfaces()
和
public interface IQueryHandlerAsync<T, in TI>
public static bool ImplementGenericInterface(this Type type, Func<Type, bool> comparer) {
return type.GetInterfaces().Any(i => i.IsGenericType
&& comparer(i.GetGenericTypeDefinition()));
}
什么是解决它的好方法?