Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity automatic injection

I have already done the following:

  1. Register an instance of my Logger in unity via a ILogger interface.
  2. Created an interface, ILoggableObject, that has a method, Hook(ILogger logger), to inject my logger.

I would like to accomplish this:

Everytime I ask for any resolution from unity, if that object implements ILoggableObject, automatically inject the ILogger interface via the Hook method.

I think this is possible via interception or policies?

Any help would be awesome.

like image 954
poindexter12 Avatar asked Oct 29 '25 16:10

poindexter12


1 Answers

What you are looking for is TypeInterception in Unity. See here: http://msdn.microsoft.com/en-us/library/ff660861(PandP.20).aspx

Also here http://msdn.microsoft.com/en-us/library/ff660848(v=PandP.20).aspx

You want to intercept the call to the constructor and inject the Logger on behalf of the calling code without them being any wiser.

While I haven't done it before I believe you can do what you want using Intercept.NewInstance() http://msdn.microsoft.com/en-us/library/ff662093(PandP.20).aspx

like image 163
Brad Cunningham Avatar answered Oct 31 '25 06:10

Brad Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!