I have a type that receives another type in its constructor, which usually is the type of the object that creates it, e.g.:
public class Logger {
public Logger(Type parent) { ... }
}
I would like to instruct Unity to resolve Logger
passing as the argument to its constructor the type of the object that requires it. Something like:
// ... would be some directive to tell Unity to use the type that
/// depends on Logger
container.RegisterType<Logger>(new InjectionConstructor(...));
So that when I try to resolve MyService
:
public MyService {
public MyService(Logger logger) { ... }
}
it will return:
var logger = new Logger(typeof(MyService));
return new MyService(logger);
Is it possible? Is there another way of doing it?
Actually, you can do this:
This is proof of concept code only, and might need to be refined a bit before production use...