本文介绍了真的无法向下转换吗?对我来说很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经发布了与同一主题相关的一些问题,但是我看到了不同的答案,因此我对哪个答案正确感到非常困惑.

I know there are already some questions posted related to this same topic, but I have seen different answers so I am quite confused on which answer is correct.

在下面的链接上,人们提到无法向下转换将基础类型转换/转换为派生类型

On the below link, people mentioned that downcasting is not possibleConvert/Cast base type to Derived type

在下面的链接上,人们提到仅当派生类是基类的实例时,才可能进行向下转换向下和向上

While on this link below, people mentioned that downcasting is possible only if the derived class is an instance of the base classdowncast and upcast

我做了一个小实验,并使用直接强制转换在小型项目(C#)上实现了向下转换,例如:DerivedClass dc =(DerivedClass)baseClass而不检查DerivedClass是否是BaseClass的实例,令我惊讶的是,一切正常,没有任何错误,为什么人们说无法向下转换?

I did a little experiment and implemented the downcasting on a mini project(C#) using direct casting, for example: DerivedClass dc = (DerivedClass) baseClass without checking whether or not DerivedClass is an instance of BaseClass, and to my surprise, it is working fine with no errors at all, so why people said downcasting is not possible?

所以我的问题是,真的不可能降职吗?为什么人们对此主题似乎有不同的答案?毕竟,哪一个是真正的答案?有人可以详细解释一下吗?

So my question is, is downcasting really not possible? Why people seem to have different answers on this topic? after-all which one is the REAL answer? Could someone please explain this in more detail?

示例代码:

public class Order {  }

public class PurchaseOrder : Order { // some new properties here for PurchaseOrder}

public static PurchaseOrder GetOrder()
{
    Order order = new Order();
    return (PurchaseOrder)order;
}

推荐答案

如果对象不是实际上您要转换为的类型的实例,则无法向下转换-如果它只是基类的一个实例.

Downcasting isn't possible when the object isn't actually an instance of the type you're trying to convert to - if it's just an instance of the base class.

当它已经是派生类的实例时,可能 .

It is possible when it's an instance of the derived class already.

例如,这很好:

object x = "hello";
string y = (string) x;

x的值是对string实例的引用,因此向下转换为string很好.

Here the value of x is a reference to an instance of string, so downcasting to string is fine.

不是很好:

object x = new object();
string y = (string) x; // Bang!

x的值是对object实例的引用,因此强制转换失败.

Here the value of x is a reference to an instance of object, so the cast fails.

您现在包含在问题中的代码就像我的 second 示例-您正在创建仅Order的实例,并尝试将其强制转换为PurchaseOrder.那是行不通的-它不是PurchaseOrder.但是,如果您这样更改它:

The code you've now included in the question is like my second example - you're creating an instance of just Order and trying to cast it to PurchaseOrder. That won't work - it isn't a PurchaseOrder. If you change it like this, however:

public static PurchaseOrder GetOrder()
{
    Order order = new PurchaseOrder();
    return (PurchaseOrder)order;
}

...很好. (这是奇怪的代码,但是可以.)

... it's fine. (It's odd code, but it works.)

有关更多信息,请参见MSDN中的铸造和类型转换"

See "Casting and type conversions" in MSDN for more information.

或如果您尝试转换为对象的 actual 类型层次结构中的其他接口或类.例如,这也可以:

Or if you're trying to convert to some other interface or class in the object's actual type's hierarchy. For example, this is okay too:

object x = new MemoryStream();
Stream y = (Stream) x; // Stream is in the type hierarchy of MemoryStream

这篇关于真的无法向下转换吗?对我来说很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:37