是否支持C#返回类型的协方差? [英] Does C# support return type covariance?

查看:146
本文介绍了是否支持C#返回类型的协方差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用.NET框架工作,我真的希望能够使我所有的网站使用网页的自定义类型。问题是当我试图从控制访问该页面。我希望能够回到我的特定类型的页面而不是默认页面。有没有办法做到这一点?

 公共类我的页面:页
{
    //我自己的逻辑
}公共类MyControl:控制
{
    公开我的页面页面{搞定;组; }
}


解决方案

这听起来像你想要的是返回类型协方差。 C#不支持返回类型协方差。

返回类型的协方差是你重写一个返回一个更具体的类型返回了一个不太确切类型的基类方法:

 抽象类外壳
{
    公共抽象动物目录();
}
水族类:外壳
{
    公众覆盖鱼内容(){...}
}

这是因为通过外壳的内容消费者期望的动物,和水族馆承诺不仅满足此项要求,但此外,为了做出更严格的承诺是安全的:该动物始终是一个鱼

这类型的协方差是不是在C#的支持,永远也不可能得到支持。它不是由CLR支持。 (它是由C ++的支持,并通过在CLR的C ++ / CLI的实现;它通过产生的那种我建议以下神奇的辅助方法,这样做)

(有些语言支持的形式参数类型的逆变,以及 - 你可以覆盖需要一个鱼接受一个动物的方法的方法同样地,合同履行;该基类要求任何鱼进行处理。和派生类承诺,不仅可以处理鱼,但任何动物。同样,C#和CLR不支持正式的参数类型逆变。)

您可以解决此限制的方法是做一些这样的:

 抽象类外壳
{
    保护动物抽象GetContents();
    公共动物目录(){返回this.GetContents(); }
}
水族类:外壳
{
    保护覆盖动物GetContents(){返回this.Contents(); }
    公开新内容的鱼(){...}
}

现在你都重写一个虚拟的方法,以及使用编译时类型水族馆的东西时,越来越强类型的好处。

I'm working with the .NET framework and I really want to be able to make a custom type of page that all of my website uses. The problem comes when I am trying to access the page from a control. I want to be able to return my specific type of page instead of the default page. Is there any way to do this?

public class MyPage : Page
{
    // My own logic
}

public class MyControl : Control
{
    public MyPage Page { get; set; }
}

解决方案

It sounds like what you want is return type covariance. C# does not support return type covariance.

Return type covariance is where you override a base class method that returns a less-specific type with one that returns a more specific type:

abstract class Enclosure
{
    public abstract Animal Contents();
}
class Aquarium : Enclosure
{
    public override Fish Contents() { ... }
}

This is safe because consumers of Contents via Enclosure expect an Animal, and Aquarium promises to not only fulfill that requirement, but moreover, to make a more strict promise: that the animal is always a fish.

This kind of covariance is not supported in C#, and is unlikely to ever be supported. It is not supported by the CLR. (It is supported by C++, and by the C++/CLI implementation on the CLR; it does so by generating magical helper methods of the sort I suggest below.)

(Some languages support formal parameter type contravariance as well -- that you can override a method that takes a Fish with a method that takes an Animal. Again, the contract is fulfilled; the base class requires that any Fish be handled, and the derived class promises to not only handle fish, but any animal. Similarly, C# and the CLR do not support formal parameter type contravariance.)

The way you can work around this limitation is to do something like:

abstract class Enclosure
{
    protected abstract Animal GetContents();
    public Animal Contents() { return this.GetContents(); }
}
class Aquarium : Enclosure
{
    protected override Animal GetContents() { return this.Contents(); }
    public new Fish Contents() { ... }
}

Now you get both the benefits of overriding a virtual method, and getting stronger typing when using something of compile-time type Aquarium.

这篇关于是否支持C#返回类型的协方差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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