Reputation: 14417
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?
Upvotes: 4
Views: 4688
Reputation: 16192
You have to use the RegisterGeneric
method see Registration Concepts - Open Generic Components
Something like this should works :
builder.RegisterGeneric(typeof(MyClass<>)).As(typeof(IMyInterface<>));
Upvotes: 9
Reputation: 31394
Yes it is trivial with 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 (e.g. MyClass) followed by the type you it to be registered as (e.g. IMyInterface).
Upvotes: 2