ASP.NET泛型三之使用協(xié)變和逆變實(shí)現(xiàn)類(lèi)型轉(zhuǎn)換
".NET泛型"系列:
協(xié)變(Convariant)和逆變(Contravariant)的出現(xiàn),使數(shù)組、委托、泛型類(lèi)型的隱式轉(zhuǎn)換變得可能。 子類(lèi)轉(zhuǎn)換成基類(lèi),稱(chēng)之為協(xié)變;基類(lèi)轉(zhuǎn)換成子類(lèi),稱(chēng)之為逆變。.NET4.0以來(lái),支持了泛型接口的協(xié)變和逆變。
泛型協(xié)變
如果子類(lèi)泛型隱式轉(zhuǎn)換成基類(lèi)泛型,使用泛型協(xié)變。
有這樣的2個(gè)基類(lèi)和派生類(lèi)。
public class Animal { public virtual void Write() { Console.WriteLine("我是基類(lèi)"); } } public class Dog : Animal { public override void Write() { Console.WriteLine("我是小小狗"); } }
為了讓派生類(lèi)Dog隱式轉(zhuǎn)換成基類(lèi)Animal,先定義支持協(xié)變的泛型接口。
//支持協(xié)變的接口 public interface IFactory<out T> { T Create(); }
再實(shí)現(xiàn)這個(gè)接口。
public class Factory<T> : IFactory<T> { public T Create() { return (T)Activator.CreateInstance<T>(); } }
客戶端調(diào)用。
class Program { static void Main(string[] args) { IFactory<Dog> dogFactory = new Factory<Dog>(); IFactory<Animal> animalFactory = dogFactory; //協(xié)變 Animal animal = animalFactory.Create(); animal.Write(); Console.ReadKey(); } }
運(yùn)行輸出:我是小小狗
以上,我們可以看出:
- 協(xié)變后,父類(lèi)的方法完全由子類(lèi)替代,父類(lèi)原先的方法不復(fù)存在
- 泛型接口中的out關(guān)鍵字必不可少
泛型逆變
關(guān)于通知的一個(gè)接口。
public interface INotification { string Message { get; } }
關(guān)于通知接口的抽象實(shí)現(xiàn)。
public abstract class Notification : INotification { public abstract string Message { get; } }
關(guān)于通知抽象類(lèi)的具體實(shí)現(xiàn)。
public class MailNotification : Notification { public override string Message { get { return "你有郵件了~~"; } } }
接下來(lái),需要把通知的信息發(fā)布出去,需要一個(gè)發(fā)布通知的接口INotifier,該接口依賴INotification,大致INotifier<INotification>,而最終顯示通知,我們希望INotifier<MailNotification>,INotifier<INotification>轉(zhuǎn)換成INotifier<MailNotification>,這是逆變,需要關(guān)鍵字in。
public interface INotifier<in TNotification> where TNotification : INotification { void Notify(TNotification notification); }
實(shí)現(xiàn)INotifier。
public class Notifier<TNotification> : INotifier<TNotification> where TNotification : INotification { public void Notify(TNotification notification) { Console.WriteLine(notification.Message); } }
客戶端調(diào)用。
class Program { static void Main(string[] args) { INotifier<INotification> notifier = new Notifier<INotification>(); INotifier<MailNotification> mailNotifier = notifier;//逆變 mailNotifier.Notify(new MailNotification()); Console.ReadKey(); } }
運(yùn)行輸出:你有郵件了~~
以上,我們可以看出:
- INotifier的方法Notify()的參數(shù)類(lèi)型是INotification,逆變后把INotification類(lèi)型參數(shù)隱式轉(zhuǎn)換成了實(shí)現(xiàn)類(lèi)MailNotificaiton。
- 泛型接口中的in關(guān)鍵字必不可少
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章:
1. ASP.NET MVC限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請(qǐng)求次數(shù)2. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車(chē)3. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移4. ASP.NET MVC把數(shù)據(jù)庫(kù)中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字5. ASP.NET延遲調(diào)用或多次調(diào)用第三方Web API服務(wù)6. ASP.NET MVC實(shí)現(xiàn)城市或車(chē)型三級(jí)聯(lián)動(dòng)7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)8. ASP.NET MVC使用JSAjaxFileUploader插件實(shí)現(xiàn)單文件上傳9. 如何將asp.net core程序部署到Linux服務(wù)器10. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解
