• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    动态代理的5模式使用示例和Mixin模式

    重量级的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.CreateClassProxyAnimal>(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.CreateClassProxyWithTargetAnimal>(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.CreateInterfaceProxyWithoutTargetIPlayable>(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.CreateInterfaceProxyWithTargetIPlayable>(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.CreateInterfaceProxyWithTargetInterfaceIPlayable>(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.CreateClassProxyAnimal>(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);
                    }
                }



     


    您可能感兴趣的文章:
    • 代理模式之Java动态代理实现方法
    上一篇:.NET命令行解析器示例程序(命令行选项功能)
    下一篇:c#加密类使用方法示例
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

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