castle dynamic proxy creation

拈花ヽ惹草 提交于 2019-12-03 08:01:31

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.

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!