重量级的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模式  

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?