将带有通用接口的类添加到DI中
如何将Imyinterface的每个实现注册到DI容器中?
而不是必须手动注册实现通用接口的每个类,我希望反思在启动时处理此操作。
public interface IMyInterface<T> { }
public class Foo : IMyInterface<Bar> { }
public class Baz : IMyInterface<A> { }
如何注册foo
和baz
作为services.addtransient&lt; imyinterface&lt; bar&gt;,foo&gt;()
使用反射?
how do I register each implementation of IMyInterface<T>
to the DI container?
Instead of having to manually register each class that implements a generic interface I want reflection to take care of this on startup.
public interface IMyInterface<T> { }
public class Foo : IMyInterface<Bar> { }
public class Baz : IMyInterface<A> { }
How do I register Foo
and Baz
as services.AddTransient<IMyInterface<Bar>, Foo>()
using reflection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自动注册您的类型分为三个步骤:
,并可以通过
assembly
进行注册。 API,通常是通过调用assembly.getTypes()
或 assembly.getExportedTypes()(查看方法最适合您的文档)。但是首先,您需要访问汇编
实例或实例。可以使用appDomain.currentdomain.getAssemblies()
来完成此操作需求,例如,您可以通过调用typeof(foo)来获得特定类型的组件.sembly
。以下代码:
下一步,您要过滤不实现
imyinterface&lt; t&gt;
的类型。这是最棘手的部分。由于以下内容将使您启动:此代码:
bar&lt&lt; t&gt;
,因为它们通常需要特别注意和注册注册
集合中产生一个项目。如果类型不实现iMyinterface&lt; t&gt;
,则它将不会成为registrations
集合的一部分,因此未注册(请参见下文)。如果类型多次实现iMyinterface&lt; t&gt;
,它会为该接口的每个闭合版本都有一个注册(根据您的需求,这非常方便)。现在,您有一份注册列表。剩下的任务是这些服务到实施映射的实际注册,这实际上是简单的部分:
etvoilà。那里有它。您的所有非传播实现
imyinterface&lt; t&gt;
均由其封闭的imyinterface&lt; t&gt;
注册。Auto-Registering your types is done in three steps:
Requesting types from the type system can be done through the
Assembly
API, typically by callingAssembly.GetTypes()
orAssembly.GetExportedTypes()
(look at the documentation which method suits you best). But first you need to access theAssembly
instance or instances. This can be done usingAppDomain.CurrentDomain.GetAssemblies()
to get all currently loaded assemblies (warning: this might not be all assemblies your application needs, or you can get the assembly of a specific type, for instance by callingtypeof(Foo).Assembly
. This is useful if you know that all types live in the same assembly.This leads to the following code:
Next, you want to filter out the types that do not implement
IMyInterface<T>
. This is the most tricky part, because Reflection and generic types can be a PITA. However, a filter such as the following will get you started:This code:
Bar<T>
, because they typically require special attention and registration.IMyInterface<T>
.registrations
collection. If a type doesn't implementIMyInterface<T>
, it will not be part of theregistrations
collection and, therefore, not registered (see below). In case a type implementsIMyInterface<T>
multiple times, it gets a registration for each closed version of that interface (which can be very handy, depending on your needs).Now you have a list of registrations to make. The remaining task is the actual registration of these service-to-implementation mappings, which is actually the easy part:
Et voilà. There you have it. All your non-generic implementations of
IMyInterface<T>
are registered by their closedIMyInterface<T>
.