类型铸造 [英] Type Casting
问题描述
您好,
有没有人知道如何做以下事情?
我有A级和B级,100%继承自A类(这意味着
我在B类中没有其他代码,而不是继承声明)。
现在我想在类型A的对象上进行类型转换,这样对象
就变成了B类型。
我试过了,但是得到错误''System.InvalidCastException:指定
强制转换无效。'',所以想要我失踪。
请给出提示。
$ b $bRené
" ;RenéNordby <重** @ bluedot.dk>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
我有一个A类和一个B类,100%继承自A类(这个
意味着我在B类中没有其他代码,而不是继承语句。)
现在我想在类型A的对象上进行类型转换,这样
对象就变成了B类型。
我已经尝试过它,但得到了错误'' System.InvalidCastException:
指定的演员表是无效的。'',所以想要我错过。
你错过了它可以'不行。你可以将派生类
转换为它的基类,但你不能将基类强制转换为派生类。
如果我是谁,有人会纠正我错误。
你好Jeff,
感谢您的快速回答。
当你这样说的时候,我就会发现String和Integer的数据类型,你可以将
从String转换为Integer,反之亦然,那么
是如何完成的?
我知道String和Integer都是从Object派生的,所以我也试过让我的两个类继承自基类,但是它给我
相同的结果。
Jeff Johnson [MVP:VB]" <我*** @ enough.spam>在留言中写道
news:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
RenéNordby <重** @ bluedot.dk>在留言中写道
新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
我有A类和B类,100%继承自A类(这意味着我在B类中没有其他代码,而不是继承语句。)
现在我想在类型A的对象上进行类型转换,以便
对象成为B类型。
>我已经试过了,但得到了错误''System.InvalidCastException:
指定的cast无效。'',所以想要我错过。
你错过了无法完成的事实。你可以将派生的
类强制转换为它的基类,但你不能将基类强制转换为派生类。
有人纠正我,如果我是错误。
你好Jeff,
你错过了它可以的事实不行。你可以将派生的
类强制转换为它的基类,但你不能将基类强制转换为派生类。
如果我错了,有人会纠正我。
您确实可以将基类引用转换为子类引用,并且
操作将导致缩小转换。但是,如果你想要投射的基类参考是
最初是从早期从子类扩展转换到
$,那么转换将会成功。 b $ b base-class。
Hi there,
Is there anyone that knows how to do the following?
I have a class A and a class B, that 100% inherits from class A (this means
that I don''t have other code in class B, than the Inherit statement).
Now I want to make a Type Casting on an object of the type A, so that object
become a B type.
I have tryied it, but gets the error ''System.InvalidCastException: Specified
cast is not valid.'', so want am I missing.
Please give a hint.
René
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class A and a class B, that 100% inherits from class A (this means that I don''t have other code in class B, than the Inherit statement).
Now I want to make a Type Casting on an object of the type A, so that object become a B type.
I have tryied it, but gets the error ''System.InvalidCastException: Specified cast is not valid.'', so want am I missing.
You''re missing the fact that it can''t be done. You can cast a derived class
into its base class, but you can''t cast a base class to a derived class.
Someone correct me if I''m wrong.
Hi Jeff,
Thanks for your quick answer.
When you say so, then I thnink of String and Integer datatypes, where you
can cast from String to Integer and the other way around, so how is that
done?
I know that both String and Integer is derived from Object, so I have also
tryied to let my two classes inherit from a base class, but it give me the
same result.
"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...I have a class A and a class B, that 100% inherits from class A (this meansthat I don''t have other code in class B, than the Inherit statement).
Now I want to make a Type Casting on an object of the type A, so that
objectbecome a B type.
I have tryied it, but gets the error ''System.InvalidCastException:
Specifiedcast is not valid.'', so want am I missing.
You''re missing the fact that it can''t be done. You can cast a derived
class into its base class, but you can''t cast a base class to a derived class.
Someone correct me if I''m wrong.
Hi Jeff,
You''re missing the fact that it can''t be done. You can cast a derived class into its base class, but you can''t cast a base class to a derived class.
Someone correct me if I''m wrong.
You can indeed cast a base class reference to a sub-class reference and the
operation would result in a ''narrowing'' conversion. BUT the conversion will
succeed ONLY if the base class reference you are trying to cast was
originally obtained from an earlier widening conversion from sub-class to
base-class.
这篇关于类型铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!