castle dynamic proxy creation

前端 未结 2 1402
悲&欢浪女
悲&欢浪女 2021-02-10 03:48

I am implementing a design where my layer would sit between client and server, and whatever objects i get from server, i would wrap it in a transparent proxy and give to the cli

相关标签:
2条回答
  • 2021-02-10 04:37

    Castle Dynamic Proxy 3.x or later can do that, although you have to keep in mind that it can only intercept virtual methods so it's not a perfect abstraction.

    0 讨论(0)
  • 2021-02-10 04:40

    We use stateless entities, and due to a behaviour of ASP.NET GridView I needed to create a proxy which would only wrap existing object.

    I created an interceptor which keeps a target instance this way:

    public class ForwardingInterceptor : IInterceptor
    {
        private object target;
    
        private Type type;
    
        public ForwardingInterceptor(Type type, object target)
        {
            this.target = target;
        }
    
        public void Intercept(IInvocation invocation)
        {
            invocation.ReturnValue = invocation.Method.Invoke(this.target, invocation.Arguments);
        }       
    }
    

    Then you can simply create the wrapper proxy:

    this.proxyGenerator.CreateClassProxy(type, new ForwardingInterceptor(type, target));
    
    0 讨论(0)
提交回复
热议问题