C#学习教程:从AutofacBuilder中获取所有AsClosedTypesOf注册变体{voidHandle(Targ);}publicclassSomeCommandHandler:ICommandHandler{voidHandle(SomeCommandarg){/*dosomething*/}}publicinterfaceICommandBus{voidRegisterHandler(Tt)whereT:ICommandHandler;voidRegisterHandlerByParam(ICommandHandlert2)whereT2:ICommand;voidRegisterHandlerMethod(Actionaction)whereT3:ICommand}publicclassTheCommandBus:ICommandBus{//implementsICommandBus...}我想自动注册ICommandHandler的所有实现。所有变体(Register*)都是有效的解决方案,即使我更喜欢Action参数,它也更灵活并且不依赖于Handler接口(只是动作委托)。Autofac能够针对程序集扫描已注册的类型,并注册找到的通用接口实现,例如:builder.RegisterAssemblyTypes(Assembly.LoadFrom("MyAssembly.dll")).AsClosedTypesOf(typeof(ICommandHandler));所以我已经注册了所有的实现。现在我需要将它们全部自动注册到TheCommandBus。怎么做?我可以通过添加以下行手动执行此操作(例如在OnActivated期间):builder.RegisterType().As().OnActivated(args=>{//现在我需要在这里列出所有实现!!!拜托,不......args.Instance.RegisterHandler(args.Context.Resolve());//在我看来并没有比以前好...args.Instance.RegisterHandlerByParam(args.Context.Resolve())//使用委托,但仍需要列出所有变体args.Instance.RegisterHandlerMethod(args.Context.Resolve().Handle)});如果我想在注册期间在lambda表达式中使用我对此类类型有疑问,我需要确定具体类型,例如这个关于另一个组件的激活过程的示例。但我不想手动列出所有这些......自动想要这样的东西。如何捕获所有ICommandHandler实现并使用Register*方法自动注册它们?编辑:另一种变体是扩展SomeCommandHandler类以在其构造函数中解析时注册自身:publicSomeCommandHandler(ICommandBuscommandBus){//并在此处注册,例如commandBus.RegisterHandlerbyParam(this);AsClosedTypesOf注册结果提供AutoActivate()。(一种可能的解决方案,但现在“处理程序”有两个职责......注册和处理)这是一个有趣且棘手的问题。泛型肯定会增加复杂性,因为非泛型将是一个简单的IEnumerable解析。但是……我想我能帮上忙。您将利用...这是一个完整的工作示例,展示了如何做到这一点。请注意,我必须更改RegisterHandler的ICommandBus接口,因为它不会以最初列出的形式为我编译,但您应该能够根据您的需要进行调整。我在ScriptCs中运行它来验证。以上就是C#学习教程:从AutofacBuilder获取所有AsClosedTypesOf注册变体分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——usingSystem;使用System.Collections.Generic使用System.Linq;使用System.Reflection;使用Autofac;公共接口ICommand{}公共类CommandOne:ICommand{}公共类CommandTwo:ICommand{}公共接口ICommandHandler其中T:ICommand{voidHandle(Targ);类CommandOneHandler:ICommandHandler{publicvoidHandle(CommandOnearg){}}publicclassCommandTwoHandler:ICommandHandler{publicvoidHandle(CommandTwoarg){}}}voidRegisterHandler(THandler)wherehandlerICommandHandlerwhereTCommand:ICommand;}publicclassCommandBus:ICommandBus{privatereadonlyList_handlers=newList();publicIEnumerableHandlers{get{returnthis._handlers;}}publicvoidRegisterHandler(THandlerhandler)其中THandler:ICommandHandler其中TCommand:ICommand{this._handlers.Add(handler);}}varbuilder=newContainerBuilder();//跟踪已注册的命令类型列表。varregisteredHandlerTypes=newList();builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsClosedTypesOf(typeof(ICommandHandler)).OnRegistered(e=>registeredHandlerTypes.Add(e.ComponentRegistration.Activator.LimitType));//通过在激活时注册处理程序来初始化总线。builder.RegisterType().As().OnActivating(e=>{foreach(varhandlerTypeinregisteredHandlerTypes){//由于泛型方法,会发生一些疯狂的反射。//首先,获取ICommandHandler接口。varhandlerInterfaceType=handlerType。GetInterface("ICommandHandler`1");//从ICommandHandler中获取。varcommandType=handlerInterfaceType.GetGenericArguments()[0];//构建RegisterHandler的封闭通用版本。varregisterMethod=typeof(ICommandBus).GetMethod("注册处理程序").MakeGenericMethod(commandType,handlerType);//调用关闭的通用RegisterHandler来注册处理程序。registerMethod.Invoke(e.Instance,newobject[]{e.Context.Resolve(handlerInterfaceType)});}}).SingleInstance();varcontainer=builder.Build();使用(varscope=container.BeginLifetimeScope()){varbus=scope.Resolve();foreach(vartinbus.Handlers){//列出已注册的处理程序类型。Console.WriteLine(t.GetType());}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: