问题
I am looking for a way to convert the following closed generics statement to open generic statement, i.e. I don't want repeat the same for entities like User, Employer, etc.
Closed type using User:
UnityContainer.RegisterType<Func<IDataContextAdapter, IRepository<User>>>(
new InjectionFactory(c =>
new Func<IDataContextAdapter, IRepository<User>>(
context => new Repository<User>(context))
)
);
I tried converting to Open generics by applying typeof(..) operation, but didn't had much success.
Any Ideas?
回答1:
Make generic Function
public void Bla<T>() where T:class
{
UnityContainer.RegisterType<Func<IDataContextAdapter, IRepository<T>>>(
new InjectionFactory(c =>
new Func<IDataContextAdapter, IRepository<T>>(
context => new Repository<T>(context))
)
);
}
来源:https://stackoverflow.com/questions/5344195/converting-from-closed-generics-to-open-generics