".NET泛型"系列:
ASP.NET泛型一之泛型简介与基本语法
ASP.NET泛型二之泛型的使用方法
ASP.NET泛型三之使用协变和逆变实现类型转换
ASP.NET泛型四之使用Lazy<T>实现延迟加载
协变(Convariant)和逆变(Contravariant)的出现,使数组、委托、泛型类型的隐式转换变得可能。 子类转换成基类,称之为协变;基类转换成子类,称之为逆变。.NET4.0以来,支持了泛型接口的协变和逆变。
泛型协变
如果子类泛型隐式转换成基类泛型,使用泛型协变。
有这样的2个基类和派生类。
public class Animal
{
public virtual void Write()
{
Console.WriteLine("我是基类");
}
}
public class Dog : Animal
{
public override void Write()
{
Console.WriteLine("我是小小狗");
}
}
为了让派生类Dog隐式转换成基类Animal,先定义支持协变的泛型接口。
//支持协变的接口
public interface IFactory<out T>
{
T Create();
}
再实现这个接口。
public class Factory<T> : IFactory<T>
{
public T Create()
{
return (T)Activator.CreateInstance<T>();
}
}
客户端调用。
class Program
{
static void Main(string[] args)
{
IFactory<Dog> dogFactory = new Factory<Dog>();
IFactory<Animal> animalFactory = dogFactory; //协变
Animal animal = animalFactory.Create();
animal.Write();
Console.ReadKey();
}
}
运行输出:我是小小狗
以上,我们可以看出:
- 协变后,父类的方法完全由子类替代,父类原先的方法不复存在
- 泛型接口中的out关键字必不可少
泛型逆变
关于通知的一个接口。
public interface INotification
{
string Message { get; }
}
关于通知接口的抽象实现。
public abstract class Notification : INotification
{
public abstract string Message { get; }
}
关于通知抽象类的具体实现。
public class MailNotification : Notification
{
public override string Message
{
get { return "你有邮件了~~"; }
}
}
接下来,需要把通知的信息发布出去,需要一个发布通知的接口INotifier,该接口依赖INotification,大致INotifier<INotification>,而最终显示通知,我们希望INotifier<MailNotification>,INotifier<INotification>转换成INotifier<MailNotification>,这是逆变,需要关键字in。
public interface INotifier<in TNotification> where TNotification : INotification
{
void Notify(TNotification notification);
}
实现INotifier。
public class Notifier<TNotification> : INotifier<TNotification> where TNotification : INotification
{
public void Notify(TNotification notification)
{
Console.WriteLine(notification.Message);
}
}
客户端调用。
class Program
{
static void Main(string[] args)
{
INotifier<INotification> notifier = new Notifier<INotification>();
INotifier<MailNotification> mailNotifier = notifier;//逆变
mailNotifier.Notify(new MailNotification());
Console.ReadKey();
}
}
运行输出:你有邮件了~~
以上,我们可以看出:
- INotifier的方法Notify()的参数类型是INotification,逆变后把INotification类型参数隐式转换成了实现类MailNotificaiton。
- 泛型接口中的in关键字必不可少