欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

动态代理的5模式使用示例和Mixin模式

程序员文章站 2024-02-26 22:28:22
重量级的orm和ioc产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一般的规律和模式还是有必要的,比如:虽然你开发期间采用了poco,...

重量级的orm和ioc产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一般的规律和模式还是有必要的,比如:虽然你开发期间采用了poco,因为开启了动态代理,运行期间则不是poco。本文简单描述了5种代理生成模式和1种mixin模式,最后给出一个示例。

复制代码 代码如下:

public interface iplayable
    {
        void play();
    }

    public class animal : iplayable
    {
        public virtual void play()
        {
            console.writeline("animal.play");
        }
    }

    public class dog : animal
    {
        public override void play()
        {
            console.writeline("dog.play");
        }
    }

    public interface irunable
    {
        void run();
    }

    public class runability : irunable
    {
        public void run()
        {
            console.writeline("runability.run");
        }
    }

    public class animalinterceptor : iinterceptor
    {
        public void intercept(iinvocation invocation)
        {
            console.writeline("before animalinterceptor.intercept");
            if (invocation.invocationtarget != null)
            {
                invocation.proceed();
            }
            console.writeline("after animalinterceptor.intercept");
        }
    }

第一种:classproxy

复制代码 代码如下:

{
                console.writeline("\n*************classproxy*************\n");
                var generator = new proxygenerator();
                var animal = generator.createclassproxy<animal>(new animalinterceptor());
                animal.play();

                console.writeline(animal.gettype());
                console.writeline(animal.gettype().basetype);

                var compositefield = animal.gettype().getfield("__target");
                console.writeline(compositefield);

                foreach (var interfacetype in animal.gettype().getinterfaces())
                {
                    console.writeline(interfacetype);
                }
            }


动态代理的5模式使用示例和Mixin模式

第二种:classproxywithtarget

复制代码 代码如下:

{
                console.writeline("\n*************classproxywithtarget*************\n");
                var generator = new proxygenerator();
                var animal = generator.createclassproxywithtarget<animal>(new dog(), new animalinterceptor());
                animal.play();

                console.writeline(animal.gettype());
                console.writeline(animal.gettype().basetype);

                var compositefield = animal.gettype().getfield("__target");
                console.writeline(compositefield);

                foreach (var interfacetype in animal.gettype().getinterfaces())
                {
                    console.writeline(interfacetype);
                }
            }



动态代理的5模式使用示例和Mixin模式

第三种:interfaceproxywithouttarget

复制代码 代码如下:

{
                console.writeline("\n*************interfaceproxywithouttarget*************\n");
                var generator = new proxygenerator();
                var animal = generator.createinterfaceproxywithouttarget<iplayable>(new animalinterceptor());
                animal.play();

                console.writeline(animal.gettype());
                console.writeline(animal.gettype().basetype);

                var compositefield = animal.gettype().getfield("__target");
                console.writeline(compositefield);

                foreach (var interfacetype in animal.gettype().getinterfaces())
                {
                    console.writeline(interfacetype);
                }
            }



动态代理的5模式使用示例和Mixin模式

第四种:interfaceproxywithtarget

复制代码 代码如下:

{
                console.writeline("\n*************interfaceproxywithtarget*************\n");
                var generator = new proxygenerator();
                var animal = generator.createinterfaceproxywithtarget<iplayable>(new dog(), new animalinterceptor());
                animal.play();

                console.writeline(animal.gettype());
                console.writeline(animal.gettype().basetype);

                var compositefield = animal.gettype().getfield("__target");
                console.writeline(compositefield);

                foreach (var interfacetype in animal.gettype().getinterfaces())
                {
                    console.writeline(interfacetype);
                }
            }



动态代理的5模式使用示例和Mixin模式

第五种:interfaceproxywithtargetinterface

复制代码 代码如下:

{
                console.writeline("\n*************interfaceproxywithtargetinterface*************\n");
                var generator = new proxygenerator();
                var animal = generator.createinterfaceproxywithtargetinterface<iplayable>(new dog(), new animalinterceptor());
                animal.play();

                console.writeline(animal.gettype());
                console.writeline(animal.gettype().basetype);

                var compositefield = animal.gettype().getfield("__target");
                console.writeline(compositefield);

                foreach (var interfacetype in animal.gettype().getinterfaces())
                {
                    console.writeline(interfacetype);
                }
            }



动态代理的5模式使用示例和Mixin模式

mixin模式

复制代码 代码如下:

{
                console.writeline("\n*************mixin*************\n");
                var generator = new proxygenerator();
                var options = new proxygenerationoptions();
                options.addmixininstance(new runability());
                var animal = generator.createclassproxy<animal>(options, new animalinterceptor());
                animal.play();
                (animal as irunable).run();

                console.writeline(animal.gettype());
                console.writeline(animal.gettype().basetype);

                var compositefield = animal.gettype().getfield("__target");
                console.writeline(compositefield);

                foreach (var field in animal.gettype().getfields())
                {
                    if (field.name.startswith("__mixin"))
                    {
                        console.writeline(field);
                    }
                }

                foreach (var interfacetype in animal.gettype().getinterfaces())
                {
                    console.writeline(interfacetype);
                }
            }



动态代理的5模式使用示例和Mixin模式