Hi,

Thanks for a great project. In one of my cases I use dynamic proxy to 
generate implementation of interface aggregating other interfaces where 
each of these other interfaces has its own implementation, so aggregate 
interface implementation is just based on composition of other interface 
implementations. Surely it could be just implemented by hand but using 
dynamic proxy saves me work when something is added to these particular 
interfaces. So I made interceptor like below. My question is, is there 
perhaps some built-in mechanism for these kind of scenario and if not, is 
there any reason for that (like do you see some risk in code like below 
[except for TODO comment part] )?

   public class MultiTargetInterceptor : IInterceptor
    {
        object[] targets;
        Dictionary<object, IEnumerable<MethodInfo>> methodCache;

        public MultiTargetInterceptor(params object[] targets)
        {
            this.targets = targets;
            this.methodCache = new Dictionary<object, 
IEnumerable<MethodInfo>>();

            foreach (var t in this.targets)
            {
                var type = t.GetType();
                var allMethods = 
type.GetMethods().Union(type.GetInterfaces().SelectMany(x => 
x.GetMethods()));
                methodCache.Add(t, allMethods);
            }
        }

        public void Intercept(IInvocation invocation)
        {
            object result;
            MethodInfo method = invocation.Method;
            if (invocation.Method.IsGenericMethod)
                method = invocation.Method.GetGenericMethodDefinition();

            var target = methodCache.Single(x => 
x.Value.Contains(method)).Key; //TODO: explicit interface implementation 
support
           
            result = invocation.GetConcreteMethod().Invoke(target, 
invocation.Arguments);

            if (invocation.Method.ReturnType != typeof(void))
                invocation.ReturnValue = result;
        }
    }


Thanks in advance,
Milosz

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to castle-project-users+unsubscr...@googlegroups.com.
To post to this group, send email to castle-project-users@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to