lundi 27 mars 2017

How to get the type of the object that is being injected in Unity?

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?





Aucun commentaire:

Enregistrer un commentaire