背后的故事之快樂的Lambda表達式二_第1頁
背后的故事之快樂的Lambda表達式二_第2頁
背后的故事之快樂的Lambda表達式二_第3頁
背后的故事之快樂的Lambda表達式二_第4頁
背后的故事之快樂的Lambda表達式二_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、背后的故事之-快樂的Lambda表達式(二)Lambda表達式玩轉(zhuǎn)姜態(tài)Lambda如何實現(xiàn)賽態(tài)?我們用抽象類和虛方法了,為什么還要用Lambda這個玩意?且看 下面的代碼:C#class MyBaseClasspublic Action SomeAction get, protected set, public MyBaseClassOSome A chon = 0 =>/Do somethingi,class MyliiheritedClass MyBaseClasspublic MylnhentedClassOSome A chon = 0 => /Do sometlung d

2、ifferent!,class MyBaseClasspublic Action SomeAction get, protected set, public MyBaseClassOSome A chon = 0 =>/Do somethingi;class MylnheritedClass MyBaseClasspublic MylnhmtedClassOSomeAction = 0 => /Do sometliing different!,我們的基類不是抽象類,也沒有虎方法,但是把屬性通過委托的方式暴露出來,然后在子類 中匝新為我們的SomeAction 予一個新的表達式。這就

3、是我們實現(xiàn)多態(tài)的過程,當然父類 中的SomeAction的set fj' protected的保護級別,不然就會被外部隨易修改了。但是這還不 完美,父類的SomeAction在子類中被覆蓋之后,我們徹底訪問不到它了,要知道真實情況 是我們町以通過base來訪問父類原來的方法的。接卜來就是實現(xiàn)這個了C# class MyBaseClasspublic Action SomeAction get, pnvate set, Stack<Action> previous Actions,protected void AddSomeAction(Action newMethod) p

4、reviousActions Push(S omeAction)t SomeAction = newMethod,protected void RemoveSomeActionOif(previousActions Count = 0)return,SomeAction = previousActions.PopO,public MyBaseClassQpreviousActions = new Stack<Action>0>Some Action = 0 => /Do sometliing!,class MyBaseClasspublic Action Some Ac

5、tion get, pnvate set, Stack<Action> previous Actionsfprotected void AddSomeAction(Action newMethod)previousActions Push(S ome Action),SomeAction = newMethod,protected void RemoveSomeActionOif(previousActions Count = 0)return,SomeAction = previousActions PopO,public MyBaseClassOpreviousActions

6、= new Stack<Action>0»SomeAction = 0 =/Do somethingi»上面的代碼中,我們通過AddSomeAction來實現(xiàn)覆溢的同時,將原來的方法保存在 previousActions中。這樣我們就可以保持兩者同時存在了。人家知道子類是不能覆蓋父類的靜態(tài)方法的,但是假設(shè)我們想實現(xiàn)靜態(tài)方法的覆蓋呢?C# void MainQvar mother = HotD aughtei' Activ atorO Messa ge,/mother =111 am the mother1var create = new HotDaugh

7、tei*0,var daughter = HotDaughter Activ atorO Message,/daughter = BI am the daughter1class CoolMotherpublic static Fuiic<Coolhlotlier> Activator get, protected set, /We are only doing this to avoid NULL references!static CoolMotherOActivator = 0 => new CoolMotherO,public CoolMotherO/Message

8、of eveiy motherMessage = "I am tlie mother1,public string Message get, protected set, class HotDaughtei' Coolhlotherpublic HotDaughtei'O/Once this constructor has been touched1 we set the Activator Activator = 0 => new HotDaughterO,/Message of eveiy daughtei*Message = MI am tlie daug

9、hter*,voidMainOvar mother = HotD aughtei* Activ atorO Message,/mother =111 am the mother"var create = new HotDaughtei'O,var daughter = HotDaughter Activ atorO Message,/daugliter = BI am the daughter*class CooMotherpublic static Func<Coolhfotlier> Activator get. protected set, /We are

10、only doing tins to avoid NULL refei'encesistatic CoolMotherOActivator = 0 => new CoolMotherO,public CoolMotherO/Message of eveiy motherMessage = "I am the mother-,public string Message get, protected set, class HotDaughtei* Coolhlotherpublic HotDaughtei'O/Once this constructor has be

11、en "touched* we set the Activator .Activator = 0 => new HotDaughtei'O,/Message of eveiy daughterhlessage = "I am tlie daughter",這里還是利用了將Lambda表達式作為屬性,町以隨時重新賦值的特點。當然這只是一個簡 單的示例,真實項目中并不建議人家這么去做。方法字典實際上這個模式我們在上一篇的返回方法中已經(jīng)講到了,只是沒有這樣一個名字而已,就算 是一個總結(jié)吧。故事是這樣的,你是不是經(jīng)常會寫到switch-case語句的時候覺得

12、不夠優(yōu)雅? 但是你又不想去整個什么工廠模式或者策略模式,那怎么樣讓你的代碼看起來高級一點呢?public Action GetFmalizer(sti'ing input)switchcase "random":return ()=>/. / ,case11 dynamic":return ()=>/* . / ,defaultreturn ()=>/. / ,/變身之后Dictionaiy<sfa'ing, Action> finalizers,public void BuildFinalizersOfinalize

13、rs = new Dictionary vstring, Action。, finalizers AddCrandom11, ()=>/ . */ ); finalizers Add("dynamic"t ()=>/* . / ).public Action GetFinahzer(sti'ing input)if(fuializei-s ContainsKey(input) return finalizersinput,return ()=>/ . */ ,public Action GetFmalizer(sti'ing input)s

14、witchcase "random":return ()=>/. / ,case11 dynamic1':return ()=>/* . / ,defaultreturn ()=>/. / ,/變身之后Dictionaiy<sfa'ing> Action> finalizei's,public void BuildFinalizersOfinalizeis = new Dictionaryvstring, Action>0,finalizers AddCrandom11,0 => /* */ ),fma

15、lizers Add("dynamic"t 0 => /*. */ ),public Action GetFinalizer(string input)if(finahzers ContainsKey(input)return fiiializersinput,return 0 => / . */ ,好像看起來是不一樣了,有那么一點味道。但是一想是所有的方法都要放到那個 BuildFinalizei-s里面,這種組織方法實在是難以接受,我們來學學插件開發(fā)的方式,讓它自 己去找所有我們需要的方法。C#static Dictionaryvstnng, Action&

16、gt; finalizers,/在靜態(tài)的構(gòu)造函數(shù)用調(diào)用這個方法public static void BuildFinalizersOfinalizes = new DictionaryVstnng, Action。,/獲得當前運行程序集下所有的類型var types = Assembly GetExecutingAssemb lyO, GetTyp es()»foreacli(vai' type in types)檢查類型,我們町以提前定義接11或抽象類 if(type.IsSubclassOf(typeof(hlyMothei-Class) /獲得默認無參構(gòu)造函數(shù)var m

17、 = type GetConstructor(Type EmptyTypes),/調(diào)用這個默認的無參構(gòu)造函數(shù)if(m i=null) var instance = m Invoke(null) as MyMotlierClass, var nanie = type Name Removed Jdother"), var method = instance Method, finalizers Add(name, method),public Action GetFmalizer(stnng input)if(finalizers ContainsKey(input)return fm

18、alizersinput,return ()=>/*/ ,static Dictionaryvstring, Action> finalizers,/在靜態(tài)的構(gòu)造兩數(shù)用調(diào)用這個方法public static void BuildFinalizersf)finalizers = new Dictionary<sti*ing, Action。,/獲得當前運行程序集F所有的類型var types = Assembly GetExecutingAssemblyO.GetTypesO,foreach(var type in types)/檢查類型,我們可以提前定義接11或抽彖類 if(

19、type IsSubclassOf(typeof(MyMotherClass) /獲得默認無參構(gòu)造函數(shù)varm = type GetConstnjctor(TypeEinptyTypes),/調(diào)用這個默認的無參構(gòu)造函數(shù)if(m != null)var instance = m Invoke(null) as MyMotherClass,var name = type Name Remove(Mother1)»var method = mstance MyMethod, finalizeis Add(name, method),public Action GetFinalizer(st

20、ring input)if(fnializers. C onta insKey (input)return finalizersinput,return ()=>/ . */ ,如果要實現(xiàn)插件化的話,我們不光要能夠加截本程序集卞的方法,還要能隨時甚至運行時去 加載外部的方法,請繼續(xù)往下看:C# internal static void BuildlnitialFinalizersOfinalizes = new Dictionaryvstnng” Action>0>LoadPlugin(Assembly GetExecutingAssemblyO)> public st

21、atic void LoadPlugm(Assembly assembly)var types = assembly GetTypesO, foreacli(vai' type ni types)if(type IsSubclassOf(typeof(MyMothei'Class)var m = type GetConstnictor(Type EmptyTypes), if(m i= null)var instance = m lnvoke(null) as MyblotherClass,var name = type Name Remove(Mother1)»var method = instance MyMethod, fmalizers Add(name, method), int

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論