[英]Autofac Resolve Open Generic Interface with Open Generic Class
So I have an interface and class: 所以我有一个接口和类:
public interface IMyInterface<T> where T : ISomeEntity {}
public class MyClass<T> : IMyInterface<T>
where T : ISomeEntity {}
I will have some class that calls for it: 我会有一些课程要求它:
public class SomeClass : ISomeClass
{
public SomeClass (IMyInterface<AuditEntity> myInterface) {}
}
I've done all sorts of things to get it to register the open generic interface and class with no luck. 我已经做了各种各样的事情来让它注册开放的通用接口和类没有运气。
I just want to say something like: 我只想说:
container.RegisterType(typeof(MyClass<>)).As(typeof(IMyInterface<>));
It would be annoying if I have to go through and explicitly do something like: 如果我必须通过并明确地执行以下操作,那将是令人讨厌的:
container.RegisterType<MyClass<AuditEntity>>().As<IMyInterface<AuditEntity>>();
Shouldn't this be trivial? 这不应该是微不足道的吗?
You have to use the RegisterGeneric
method see Registration Concepts - Open Generic Components 您必须使用
RegisterGeneric
方法,请参阅注册概念 - 打开通用组件
Something like this should works : 这样的东西应该有效:
builder.RegisterGeneric(typeof(MyClass<>)).As(typeof(IMyInterface<>));
Yes it is trivial with container.RegisterGeneric: 是的,它对于container.RegisterGeneric来说是微不足道的:
container.RegisterGeneric(typeof(MyClass<>)).As(typeof(IMyInterface <>));
You also seem to have your interface and class switched in the examples in your question. 您似乎也在问题的示例中切换了您的界面和类。 The registration should start with the type to want to register (eg MyClass) followed by the type you it to be registered as (eg IMyInterface).
注册应该从想要注册的类型(例如MyClass)开始,然后是要注册的类型(例如IMyInterface)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.