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()));
}
什麼是解決它的好方法?