最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

動(dòng)態(tài)代理的5模式使用示例和Mixin模式

 更新時(shí)間:2013年11月25日 10:18:37   作者:  
什么叫"動(dòng)態(tài)代理",代理模式我們都知道,動(dòng)態(tài)代理就是動(dòng)態(tài)生成的代理(采用Emit)。5種代理模式:ClassProxy、ClassProxyWithTarget、InterfaceProxyWithoutTarget、InterfaceProxyWithTarget、InterfaceProxyWithTargetInterface、Mixin模式

重量級(jí)的ORM和IOC產(chǎn)品離不開動(dòng)態(tài)代理,作為開發(fā)人員,多數(shù)情況不用關(guān)注動(dòng)態(tài)代理的內(nèi)部實(shí)現(xiàn)機(jī)制,但是了解其一般的規(guī)律和模式還是有必要的,比如:雖然你開發(fā)期間采用了POCO,因?yàn)殚_啟了動(dòng)態(tài)代理,運(yùn)行期間則不是POCO。本文簡(jiǎn)單描述了5種代理生成模式和1種Mixin模式,最后給出一個(gè)示例。

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

{
                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

復(fù)制代碼 代碼如下:

{
                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

復(fù)制代碼 代碼如下:

{
                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

復(fù)制代碼 代碼如下:

{
                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

復(fù)制代碼 代碼如下:

{
                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模式

復(fù)制代碼 代碼如下:

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



 


相關(guān)文章

最新評(píng)論

丹阳市| 玉门市| 夏津县| 蕉岭县| 沁阳市| 泸西县| 台北市| 荔波县| 齐齐哈尔市| 南靖县| 江都市| 祁阳县| 莱阳市| 延津县| 安义县| 阿鲁科尔沁旗| 射阳县| 万源市| 堆龙德庆县| 石渠县| 黄浦区| 新兴县| 营口市| 元氏县| 清远市| 嵊州市| 西平县| 昌都县| 长乐市| 防城港市| 盱眙县| 邹平县| 峨眉山市| 曲水县| 辽源市| 通许县| 临湘市| 尉犁县| 永修县| 湘阴县| 黄大仙区|