Registering types with lambda expression

2019-05-07 09:12发布

问题:

I was wondering how do I achieve such a feature in the UnityContainer:

container.RegisterType<IDummy>(Func<IDummy>) // deferred resolution

回答1:

If you're going to register factory instead of instance, try this:

container.RegisterType<IDummy>(new InjectionFactory(context => new Dummy()));

Just replace "context => new Dummy()" with your lambda.