内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Unity控制反轉(zhuǎn)的方法是什么

本篇內(nèi)容主要講解“Unity控制反轉(zhuǎn)的方法是什么”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Unity控制反轉(zhuǎn)的方法是什么”吧!

目前累計(jì)服務(wù)客戶近千家,積累了豐富的產(chǎn)品開發(fā)及服務(wù)經(jīng)驗(yàn)。以網(wǎng)站設(shè)計(jì)水平和技術(shù)實(shí)力,樹立企業(yè)形象,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。創(chuàng)新互聯(lián)始終以務(wù)實(shí)、誠(chéng)信為根本,不斷創(chuàng)新和提高建站品質(zhì),通過對(duì)領(lǐng)先技術(shù)的掌握、對(duì)創(chuàng)意設(shè)計(jì)的研究、對(duì)客戶形象的視覺傳遞、對(duì)應(yīng)用系統(tǒng)的結(jié)合,為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。

控制反轉(zhuǎn)上層不依賴下層,而是依賴第三方依賴注入容器

上次的SimpleFactory就可以看做是第三方容器。學(xué)生student依賴SimpleFactory 而不直接依賴細(xì)節(jié)(Honor)

我們常用的第三方容器就是Unity,在VS中通過NuGet引入U(xiǎn)nity的Dll,改造我們的Main方法

            static void Main(string[] args)
        {
            {
                BasePhone honor = SimpleFactory.CreatePhone();
                IPlayPhone student = SimpleFactory.CreateStudent();
                student.PlayPhone(honor);
                
                IUnityContainer unityContainer = new UnityContainer();
                unityContainer.RegisterType<IPlayPhone, Student>();
                var studentUnity = unityContainer.Resolve<IPlayPhone>();
                studentUnity.PlayPhone(honor);
                
                //Honor honor = new Honor();
                //Student student = new Student();
                //student.PlayPhone(honor);
                //student.PlayPhone(lumiaPhone);
                //student.PlayApplePhone(applePhone);
                //student.PlayGalaxy(galaxy);
                //student.PlayHonor(honor);
            }
            Console.ReadKey();
        }

再將代碼改造下增加IPerson,Iiphone,IGame

 public interface IPerson
    {
        Iiphone Iphone { get; set; }
        IGame Game { get; set; }
        
    }
  public class Student: BasePerson, IPerson
    {
        [Dependency]
        public Iiphone Iphone { get; set; }
        [Dependency]
        public IGame Game { get; set; }

    }
    public class Teacher: BasePerson, IPerson
    {
        [Dependency]
        public Iiphone Iphone { get; set; }
        [Dependency]
        public IGame Game { get; set; }
    }
 public interface Iiphone
    {
       void UsePhone();
    }
     
     public class Galaxy:BasePhone, Iiphone
    {
        public override void System()
        {
            Console.WriteLine("ANDROID");
        }
        public void UsePhone()
        {
            Console.WriteLine("Galaxy");
        }
    }
  public interface IGame
    {
        void Game();
    }
     public class SgsGame:IGame
    {
        public void Game()
        {
            Console.WriteLine("play 三國(guó)殺Game");
        }
    }
     public class LolGame:IGame
    {
        public void Game()
        {
            Console.WriteLine("Play LoLGame");
        }
    }
    
    
     static void Main(string[] args)
        {
            {
               

                IUnityContainer unityContainer = new UnityContainer();
              
                unityContainer.RegisterType<IPerson, Teacher>();
                unityContainer.RegisterType<Iiphone, Galaxy>();
                unityContainer.RegisterType<IGame, SgsGame>();
              
                var studentUnity = unityContainer.Resolve<IPerson>();
                studentUnity.Iphone.UsePhone();
                studentUnity.Game.Game();
            }

這里用的是依賴注入中的屬性注入,屬性注入在構(gòu)造函數(shù)注入之后執(zhí)行,而且需要增加[Dependency]這個(gè)特性,并且需要添加using Microsoft.Practices.Unity;的引用,所以大部分時(shí)候都用構(gòu)造函數(shù)注入更方便, 還有方法注入項(xiàng)目中用的很少,在方法上加[InjectionMethod]特性Unity控制反轉(zhuǎn)的方法是什么

unity提供更靈活的用配置文件注冊(cè)的方法 在項(xiàng)目配置文件增加

  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity>
    <containers>
      <container name="defaultContainer">
        <register type="Abstract.IPerson,Abstract" mapTo="Abstract.Teacher, Abstract"/>
        <register type="Abstract.Iiphone,Abstract" mapTo="Abstract.Galaxy, Abstract"/>
        <register type="Abstract.IGame,Abstract" mapTo="Abstract.SgsGame, Abstract"/>
      </container>
    </containers>
  </unity>

則Main函數(shù)修改

 static void Main(string[] args)
        {
            {
                IUnityContainer unityContainer = new UnityContainer();
                UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
                configuration.Configure(unityContainer, "defaultContainer");
                
                var studentUnity = unityContainer.Resolve<IPerson>();
                studentUnity.OutputIdentity();
                studentUnity.Iphone.UsePhone();
                studentUnity.Game.Game();
            }
            Console.ReadKey();
        }

到此,相信大家對(duì)“Unity控制反轉(zhuǎn)的方法是什么”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

標(biāo)題名稱:Unity控制反轉(zhuǎn)的方法是什么
文章位置:http://www.rwnh.cn/article6/psgcog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站營(yíng)銷、網(wǎng)站排名、搜索引擎優(yōu)化、品牌網(wǎng)站制作、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站
崇文区| 东阳市| 洛隆县| 花莲市| 濮阳市| 潮安县| 白玉县| 霍州市| 金川县| 临高县| 青岛市| 西青区| 岳西县| 大埔县| 宁国市| 奇台县| 海原县| 方城县| 扶余县| 临潭县| 唐河县| 分宜县| 肥城市| 黄平县| 申扎县| 南投市| 新密市| 扎鲁特旗| 黎城县| 江津市| 金秀| 金湖县| 荆州市| 清徐县| 磴口县| 金昌市| 沁水县| 九龙县| 蓝田县| 共和县| 泽普县|