views:

113

answers:

1

Hi.

I'm trying to get StructureMap to put a Castle.DynamicProxy around some of the objects it creates. I have used the EnrichWith-feature earlier, but I think RegisterInterception would suit me better in this case, since I use scanning.

The problem is that in the "Process(object target, IContext context)"-method, I can't find out which Interface SM is trying to get, only the concrete class. I could find all the interfaces this class implements, but in case it implements more than one interface, I have no idea how to find which Interface was actually requested. Is there any way to do this?

Here's some code:

    public class SMInterceptor : TypeInterceptor
    {
        private readonly IInterceptor _interceptor;
        private readonly ProxyGenerator _proxyGenerator;


        public SMInterceptor(IInterceptor interceptor, ProxyGenerator proxyGenerator)
        {
            _interceptor = interceptor;
            _proxyGenerator = proxyGenerator;
        }

        public static List<Type> TypesToIntercept = new List<Type>();

        public object Process(object target, IContext context)
        {
            var interfaceToTarget = // This is where I want the target interface!
            var decorator = _proxyGenerator.CreateInterfaceProxyWithTarget(interfaceToTarget, target, _interceptor);
            return decorator;
        }

        public bool MatchesType(Type type)
        {
           return true;
        }
   }
A: 

A bit late, but the below code should work (given that the requested type is an instance)

var interfaceToTarget = context.BuildStack.Current.RequestedType;
Joel Abrahamsson
This doesn't work quite right. I have made a simple console app example here: http://pastie.org/1088333This returns: interface: ConsoleApplication1.IOnetarget: ConsoleApplication1.TwoAnd what I really wanted was This doesn't work quite right. I have made a simple console app example here: http://pastie.org/1088333This returns: interface: ConsoleApplication1.ITwo
gautema