summaryrefslogtreecommitdiff
path: root/lib/infrastructure/proxies/RemotingProxyFactory.cs
blob: d763fc131baddf9a00c882b7fabb55768f16c9b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using gorilla.utility;

namespace gorilla.infrastructure.proxies
{
    public class RemotingProxyFactory<T> : RealProxy
    {
        readonly T target;
        readonly IEnumerable<Interceptor> interceptors;

        public RemotingProxyFactory(T target, IEnumerable<Interceptor> interceptors) : base(typeof (T))
        {
            this.target = target;
            this.interceptors = interceptors;
        }

        public override IMessage Invoke(IMessage message)
        {
            if (message.is_an_implementation_of<IMethodCallMessage>())
            {
                var call = message.downcast_to<IMethodCallMessage>();
                var invocation = new MethodCallInvocation<T>(interceptors, call, target);
                invocation.proceed();
                return return_value(invocation.return_value, invocation.arguments, call);
            }
            return null;
        }

        IMessage return_value(object return_value, object[] out_parameters, IMethodCallMessage call)
        {
            return new ReturnMessage(return_value,
                                     out_parameters,
                                     out_parameters == null ? 0 : out_parameters.Length,
                                     call.LogicalCallContext, call);
        }

        public T create_proxy()
        {
            return GetTransparentProxy().downcast_to<T>();
        }
    }
}