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

 更新时间:2021年9月22日 10:13  点击:1388

重量级的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);
                }
            }


第二种: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);
                }
            }



第三种: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);
                }
            }



第四种: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);
                }
            }



第五种: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);
                }
            }



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);
                }
            }



 


[!--infotagslink--]

相关文章

  • javascript设计模式之解释器模式详解

    神马是“解释器模式”?先翻开《GOF》看看Definition:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。在开篇之前还是要科普几个概念: 抽象语法树: 解释器模式并未解释如...2014-06-07
  • Postgresql 如何选择正确的关闭模式

    这篇文章主要介绍了Postgresl 如何选择正确的关闭模式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-18
  • 学习JavaScript设计模式之装饰者模式

    这篇文章主要为大家介绍了JavaScript设计模式中的装饰者模式,对JavaScript设计模式感兴趣的小伙伴们可以参考一下...2016-01-21
  • 如何开启mysql中的严格模式

    很多集成的PHP环境(PHPnow WAMP Appserv等)自带的MySQL貌似都没有开启MySQL的严格模式,何为MySQL的严格模式,简单来说就是MySQL自身对数据进行严格的校验(格式、长度、类型等),比如一个整型字段我们写入一个字符串类型的数...2013-10-04
  • c#标准idispose模式使用示例

    下面将把C#里实现IDispose模式的代码展现出来,大家一起来学习一下,它的使用场合也很多的,当我们手动对网站,数据库作封装时,都会用的到...2020-06-25
  • C# MVC模式中应该怎样区分应用程序逻辑(Controller层)和业务逻辑(Model层)?

    这篇文章主要介绍了C# MVC模式中应该怎样区分应用程序逻辑(Controller层)和业务逻辑(Model层)?,这也小编做.NET项目时经常思考和让人混乱的一个问题,这篇文章写的挺好,一下清晰了许多,需要的朋友可以参考下...2020-06-25
  • JavaScript设计模式之职责链模式

    这篇文章主要介绍了JavaScript设计模式之职责链模式,对设计模式感兴趣的同学,可以参考下...2021-04-25
  • 学习JavaScript设计模式之状态模式

    这篇文章主要为大家介绍了JavaScript设计模式中的状态模式,对JavaScript设计模式感兴趣的小伙伴们可以参考一下...2016-01-12
  • php Observer观察者模式之学习笔记

    当我们在星际中开地图和几家电脑作战的时候,电脑的几个玩家相当于结盟,一旦我们出兵进攻某一家电脑,其余的电脑会出兵救援。 那么如何让各家电脑知道自己的盟友被攻击了...2016-11-25
  • Java接口DAO模式代码原理及应用详解

    这篇文章主要介绍了Java接口DAO模式代码原理及应用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-03
  • C#中的IDisposable模式用法详解

    这篇文章主要介绍了C#中的IDisposable模式用法,讲述了垃圾资源回收机制的实现,并对比分析了Dispose()方法、~DisposableClass()析构函数、虚方法Dispose(bool disposing)的原理,需要的朋友可以参考下...2020-06-25
  • 阿里云OpenSearch在php版sdk开启调试模式

    版本:php_v2.0.6 在CloudsearchClient的类中,开启debug模式,设置为true 如:$opts = array(&#39;host&#39;=>$host,&#39;debug&#39;=>true); 注意true不能加引号...2016-05-19
  • C#使用Dispose模式实现手动对资源的释放

    这篇文章主要介绍了C#使用Dispose模式实现手动对资源的释放,涉及C#采用Dispose模式操作资源的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 学习JavaScript设计模式之单例模式

    这篇文章主要为大家介绍了JavaScript设计模式中的单例模式,对JavaScript设计模式感兴趣的小伙伴们可以参考一下...2016-01-21
  • MIUI儿童模式如何开启 小米手机儿童模式开启教程

    您是否遇到过孩子想要玩手机又不好拒绝,或者是您想要给孩子配一个联系用的手机有担心孩子玩手机上瘾?如果您购买的是小米手机,那么MIUI的儿童模式就能很好地解决这个问题...2016-12-21
  • JavaScript设计模式之命令模式

    这篇文章主要介绍了JavaScript设计模式之命令模式,对设计模式感兴趣的同学,可以参考下...2021-04-25
  • PHP单例模式静态类解析和实现源代码

    我们建站的时候,开始是每次请求数据库都要重新连接的、这样显然不合理、然后自己封装了一个数据库操作类、DBTools.php、要解决一个连接多次使用的话、最好的办法是使...2016-11-25
  • vue-router history模式服务器端配置过程记录

    vue路由有hash和history两种模式,这篇文章主要给大家介绍了关于vue-router history模式服务器端配置的相关资料,需要的朋友可以参考下...2021-06-08
  • 学习JavaScript设计模式之责任链模式

    这篇文章主要为大家介绍了JavaScript设计模式中的责任链模式,对JavaScript设计模式感兴趣的小伙伴们可以参考一下...2016-01-21
  • 学习JavaScript设计模式之代理模式

    这篇文章主要为大家介绍了JavaScript设计模式中的状态模式,对JavaScript设计模式感兴趣的小伙伴们可以参考一下...2016-01-14