为什么一个泛型类型约束导致没有隐式引用转换错误? [英] Why does a generic type constraint result in a no implicit reference conversion error?

查看:1326
本文介绍了为什么一个泛型类型约束导致没有隐式引用转换错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了几个接口和泛型类与议程任命工作:

I have created a couple of interfaces and generic classes for working with agenda appointments:

interface IAppointment<T> where T : IAppointmentProperties
{
    T Properties { get; set; }
}

interface IAppointmentEntry<T> where T : IAppointment<IAppointmentProperties>
{
    DateTime Date { get; set; }
    T Appointment { get; set; }
}

interface IAppointmentProperties 
{
    string Description { get; set; }
}

class Appointment<T> : IAppointment<T> where T : IAppointmentProperties
{
    public T Properties { get; set; }
}

class AppointmentEntry<T> : IAppointmentEntry<T> where T : IAppointment<IAppointmentProperties>
{
    public DateTime Date { get; set; }
    public T Appointment { get; set; }
}

class AppointmentProperties : IAppointmentProperties
{
    public string Description { get; set; }
}



我想使用的类型参数的一些限制,以确保唯一有效的类型可以被指定。但是,指定定义一个 T 必须实现一个约束时 IAppointment< IAppointmentProperties> ,使用当编译器给出错误类是预约< AppointmentProperties>

I'm trying to use some constraints on the type parameters to ensure that only valid types can be specified. However, when specifying a constraint defining that T must implement IAppointment<IAppointmentProperties>, the compiler gives an error when using a class that is Appointment<AppointmentProperties>:

class MyAppointment : Appointment<MyAppointmentProperties>
{
}

// This goes wrong:
class MyAppointmentEntry : AppointmentEntry<MyAppointment>
{
}

class MyAppointmentProperties : AppointmentProperties
{
    public string ExtraInformation { get; set; }
}



该错误是:

The error is:

类型'Example.MyAppointment不能用作泛型类型或方法的类型参数'T''Example.AppointmentEntry< T>'。有一个从Example.MyAppointment'没有隐式引用转换Example.IAppointment< Example.IAppointmentProperties>

任何人可以解释原因。 ?这不工作

Could anybody explain why this does not work?

推荐答案

让我们简化:

interface IAnimal { ... }
interface ICage<T> where T : IAnimal { void Enclose(T animal); } 
class Tiger : IAnimal { ... }
class Fish : IAnimal { ... }
class Cage<T>  : ICage<T> where T : IAnimal { ... }
ICage<IAnimal> cage = new Cage<Tiger>();

您的问题是:为什么是最后一行非法

Your question is: why is the last line illegal?

现在,我已经重写代码来简化它,它应该是清楚的。 ICage< IAnimal> 在一个笼子可以在其中放置的任何动物的,但笼LT; TIGER> ; 可以的只持有虎的,所以这一定是非法的

Now that I have rewritten the code to simplify it, it should be clear. An ICage<IAnimal> is a cage into which you can place any animal, but a Cage<Tiger> can only hold tigers, so this must be illegal.

如果它不是非法的。那么你可以这样做:

If it were not illegal then you could do this:

cage.Enclose(new Fish());

和嘿,你只要把鱼成虎笼。

And hey, you just put a fish into a tiger cage.

该型系统不允许的转换,因为这样做会违反规则的源类型的能力绝不能的的比目标类型的能力。 (这就是著名的里氏替换原则的一种形式。)

The type system does not permit that conversion because doing so would violate the rule that the capabilities of the source type must not be less than the capabilities of the target type. (This is a form of the famous "Liskov substitution principle".)

更​​具体地说,我会说,你在滥用仿制药。你所做型关系,这是太复杂,你要分析自己的事实证据,你应该简化整个事情;如果你没有将所有的类型关系笔直,你写的东西,然后你的用户一定会无法保持直线无论是。

More specifically, I would say that you are abusing generics. The fact that you've made type relationships that are too complicated for you to analyze yourself is evidence that you ought to simplify the whole thing; if you're not keeping all the type relationships straight and you wrote the thing then your users surely will not be able to keep it straight either.

这篇关于为什么一个泛型类型约束导致没有隐式引用转换错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆