Hi Miłosz

That sounds an awful lot like mixins: 
http://kozmic.net/2009/08/12/castle-dynamic-proxy-tutorial-part-xiii-mix-in-this-mix/

-- 
Krzysztof Kozmic

From: Miłosz Kukla [email protected]
Reply: [email protected] 
[email protected]
Date: 15 May 2014 at 6:16:22 am
To: [email protected] [email protected]
Subject:  Composition based aggregate interface dynamic proxy  

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)
        {
            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
          
            var 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

-- 
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 [email protected].
To post to this group, send email to [email protected].
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