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

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

问题描述

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

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; }
}

推荐答案


更新:这个答案是在 2011 年写的.在人们为 C# 提出返回类型协变的二十年之后,他们已经实现了.请参阅 https://devblogs.microsoft.com 中的协变返回/dotnet/c-9-0-on-the-record/.

听起来您想要的是返回类型协方差.C# 不支持返回类型协方差.

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() { ... }
}

这是安全的,因为 Contents via Enclosure 的消费者期望动物,而 Aquarium 承诺不仅满足该要求,而且做出更严格的承诺:动物始终是鱼.

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.

这种协方差在 C# 中不受支持,并且不太可能被支持.CLR 不支持它.(C++ 以及 CLR 上的 C++/CLI 实现都支持它;它通过生成我在下面建议的那种神奇的辅助方法来实现.)

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.)

(一些语言也支持形式参数类型逆变——你可以用一个采用 Animal 的方法覆盖一个采用 Fish 的方法.同样,契约已经完成;基类要求处理任何 Fish,并且派生类承诺不仅可以处理鱼,还可以处理任何动物.同样,C# 和 CLR 不支持形式参数类型逆变.)

(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() { ... }
}

现在,您既可以获得覆盖虚方法的好处,又可以在使用编译时类型 Aquarium 时获得更强大的类型.

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天全站免登陆