Unity 2 Interception equivalent of CreateInterfaceProxyWithoutTarget in Castle Dynamic Proxy

Feb 7, 2011 at 8:36 AM

I want to use Unity interception for an interface without an implementation, so that the IInterceptionBehavior actually becomes the implementation. This is the same as the CreateInterfaceProxyWithoutTarget method in Castle Dynamic Proxy.
 
Is this possible in Unity?

Feb 9, 2011 at 5:36 AM

I had thought the answer was "of course", but I'm actually looking at the code and it's actually not allowed directly. Argh... will have to fix that for the next version.

There is a workaround that may suit your needs, though. You can't do this through the container, you'll need to go through the direct interception API. There's static methods on the Intercept class that do the heavy lifting of creating interceptors and wiring up the behaviors.

What I'd suggest is trying this API call:

object intercepted = Intercept.NewInstance<object>(
    new VirtualMethodInterceptor(),
    behaviors);

What this will do is derive a type from class object that includes all the interfaces implemented by your behaviors. You can then cast "intercepted" to whatever interfaces you're looking for. Actually, looking at this, it's probably exactly what you're looking for, albeit somewhat non-obvious.

Anyway, to hook it up to the container, the quickest thing to do would be to use InjectionFactory to call the Intercept.NewInstance API.

Hope this helps,

-Chris

 

Feb 11, 2011 at 3:41 PM

Thanks, but I am slightly confused. The cast of the intercepted object to my interface fails because my interceptor just implements IInterceptionBehavior, not the actual interface that I wish to intercept.