1 回答
TA贡献1828条经验 获得超3个赞
我这样做了,那是很久以前的事了,使用 Nancy、EasyQ 和 DryIOC
它不是基于属性而是基于类
将注册所有具体类的辅助方法IPipeline
RegisterMultipleTypesToContainer(container, typeof(IPipeline));
我想这就是 Scutor 所做的,但当时没有 scutor,所以必须使用 Reflection,然后订阅......
private void RegisterMultipleTypesToContainer(IContainer container, Type type)
{
var allTypes = AppDomain.CurrentDomain.GetAssemblies()
.Where(x =>
x.FullName.StartsWith("MyAssembly", StringComparison.Ordinal))SelectMany(x => x.GetTypes());
var types = allTypes.Where(x => type.IsAssignableFrom(x) && x.IsClass && !x.IsAbstract).ToList();
foreach (var concreteTypes in types)
{
var genericTypes = concreteTypes.GetInterfaces().Where(x => x != type).ToList();
foreach (var genericType in genericTypes)
{
container.Register(genericType, concreteTypes, Reuse.Singleton);
var pipeline = container.Resolve(genericType) as IPipeline;
pipeline.Subscribe();
}
}
}
public class HelloPipeline :
BasePipeline<HelloPipeline.Message>
{
IBusService _bus;
HelloPipeline(IBusService bus){
_bus = bus;
}
private Task ExecuteAsync(String channel, ) { }
public void Subscribe()
{
_bus.SubscribeAsync<T>(this.GetType().Name, this.ExecuteAsync);
}
}
刚刚检查了旧代码,但仍然有效,希望它能给您一个想法,但我认为您想要实现的目标与我所做的相同,但使用其他工具
- 1 回答
- 0 关注
- 124 浏览
添加回答
举报