主頁 > 知識(shí)庫 > 動(dòng)態(tài)代理的5模式使用示例和Mixin模式

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

熱門標(biāo)簽:怎樣給陜西地圖標(biāo)注顏色 地圖標(biāo)注多少錢一張 廣州銷售外呼系統(tǒng)定制 云狐人工智能電話機(jī)器人 400電話辦理信任翰諾科技 ai電銷機(jī)器人對(duì)貸款有幫助嗎 福州人工智能電銷機(jī)器人加盟 宿遷智能外呼系統(tǒng)排名 電銷機(jī)器人 數(shù)據(jù)

重量級(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。本文簡單描述了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.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

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

{
                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

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

{
                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

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

{
                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

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

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

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

{
                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動(dòng)態(tài)代理實(shí)現(xiàn)方法

標(biāo)簽:宜春 綿陽 延安 黃南 大興安嶺 新疆 曲靖 焦作

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《動(dòng)態(tài)代理的5模式使用示例和Mixin模式》,本文關(guān)鍵詞  動(dòng)態(tài),代理,的,模式,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《動(dòng)態(tài)代理的5模式使用示例和Mixin模式》相關(guān)的同類信息!
  • 本頁收集關(guān)于動(dòng)態(tài)代理的5模式使用示例和Mixin模式的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章