views:

48

answers:

2

Hi fellow programmers,

I am using Microsoft Unity 2.0 and the interception extension is not working as expected.

Consider these two lines of code:

MyUnityContainer.Configure<Interception>().SetDefaultInterceptorFor<MyType>(new VirtualMethodInterceptor());
var someObject = MyUnityContainer.BuildUp<MyType>(anObject);

These two lines don't get you the dynamic proxy you'd expect for someObject! How can one make interception work for such a scenario?

+1  A: 

This page explains that you cannot use virtual interception using BuildUp since it can only be applied when the object is created (since a subclass of the target object is dynamically generated):

Interception only happens on virtual methods. You must set up interception at object creation time and cannot intercept an existing object.

Lee
Is there any way to intercept existing intstances without making them cloneable? Maybe a custom object creator?
Falcon
A: 

VirtualMethodInterceptor works only on new objects. You could use the Interface or TransparentProxy interceptors instead to intercept an existing instance (since those use explicit proxy objects).

I could see possibly adding a VirtualMethodProxyInterceptor, but I expect it'd just cause more confusion than help.

Chris Tavares