InterfaceInterceptor Hack

Feb 18, 2010 at 8:00 PM

Hi Everyone,

I'm using the Validation Aspects with Unity and was trying to avoid using the TransparentProxyInterceptor because its so slow compared to the InterfaceInterceptor and VirtualInterceptor. Out of the box, the Validation Aspects don't work with the InterfaceInterceptor. It just looks like the dev just never got to supporting it. Soo... I came up with a hack to make it work.

First, I added a property in the ValdiationCallHandler:

        public bool UseInterfaceInterception { get; set; }

and then I updated the Invoke method with the following right after methodBase is resolved...

            // custom code to handle InterfaceInterception... Code only handled TransparentProxyInterception
            if (UseInterfaceInterception)
                methodBase = ((FieldInfo)input.Target.GetType().GetMember("target", BindingFlags.Instance | BindingFlags.NonPublic)[0]).FieldType.GetMethod(methodBase.Name);

Yes, its using reflection to get at a private member called target, but its all I could come up with to get at the actual type instead of the proxied one so the attribute could be retrieved.

Anyways, thought I'd share.