所以我有一个接口(interface)和类:
public interface IMyInterface<T> where T : ISomeEntity {}
public class MyClass<T> : IMyInterface<T>
where T : ISomeEntity {}
我会有一些需要它的类(class):
public class SomeClass : ISomeClass
{
public SomeClass (IMyInterface<AuditEntity> myInterface) {}
}
我做了各种各样的事情来让它注册开放的通用接口(interface)和类,但没有成功。
我只想说:
container.RegisterType(typeof(MyClass<>)).As(typeof(IMyInterface<>));
如果我必须通过并明确地做类似的事情,那会很烦人:
container.RegisterType<MyClass<AuditEntity>>().As<IMyInterface<AuditEntity>>();
这不应该是微不足道的吗?
最佳答案
您必须使用RegisterGeneric
方法参见Registration Concepts - Open Generic Components
像这样的东西应该可以工作:
builder.RegisterGeneric(typeof(MyClass<>)).As(typeof(IMyInterface<>));
关于c# - Autofac 使用开放通用类解析开放通用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31588094/