使用GetType进行类型转换 [英] type casting with GetType

查看:186
本文介绍了使用GetType进行类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




- 我有一个Node类,它有一个子类Num,它继承自Node。

- 我有一个函数Evaluate ;这需要两个节点作为参数。

- 我通过发送两个Num来调用此函数。当我使用

GetType()。ToSTring()时,它似乎意识到它收到的参数

实际上是Nums,而不是节点。


- 如何将参数转换为Nums以便我可以使用所有成员函数

和变量,而无需手动执行

,即( NUM)someVariable。我想这样做,所以我可以发送评估

许多不同的Node子,而不会使每个添加的代码进一步复杂化

。我无法弄清楚怎么做

GetType()。


帮助大多数人赞赏。

干杯

dave

hi

- I have a class Node, with a child class Num, that inherits from Node.
- I have a function "Evaluate" that takes two Nodes as an argument.
- I call this function by sending it two "Num"''s. When i use
GetType().ToSTring(), it seems to be aware that the arguments it recieved
are in fact Nums, not Nodes.

- How do I cast the arguments to Nums so I can use all the member functions
and variables, without doing
it manually, ie (Num)someVariable. I want to do this so I can send Evaluate
lots of different childs of Node, without having complicate
my code further for each addition. I can''t figure out how to do it with
GetType().

Help most appreciated.
Cheers
dave

推荐答案

如果你知道所有被解除的物品而且它们不是太多,

i建议最简单的方法是检查它是什么类型。


if(node is Num)

{

(( Num)node).Something();

}

else if(node is AnotherDerived)

{

((AnotherDerived)节点)。SomethingElse();

}


或者如果某些派生者共享相同的功能,你可以拥有另一个

基类在顶部

Node。

if(node是ExtensionNode)

{

((ExtensionNode)节点).Something();

}

或者让它们实现接口等等。


如果这不是你的选择,唯一的方法是使用反射来动态调用会员来支付


但是你需要知道要打电话的会员的名字。

-

问候,

DennisJDMyrén

Oslo Kodebureau

" David Sobey" < DS **** @ NOSPAMugrad.unimelb.edu.au>在消息中写道

news:42 ******** @ dnews.tpgi.com.au ...
If you know all the dervied objects and they are not too many,
i suggest the easiest way which is checking what type it is.

if (node is Num)
{
((Num) node).Something();
}
else if (node is AnotherDerived)
{
((AnotherDerived) node).SomethingElse();
}

or if some of the derivors share the same functions you could have another
base class on top
of Node.
if (node is ExtensionNode)
{
((ExtensionNode) node).Something();
}
or have them implement an interface or something.

If this is not an option for you, the only way is to use reflection to
dynamically call members,
but then you would need to know the name of the member to call anyway.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"David Sobey" <ds****@NOSPAMugrad.unimelb.edu.au> wrote in message
news:42********@dnews.tpgi.com.au...
hi

- 我有一个Node类,它有一个子类Num,它继承自Node。
- 我有一个函数Evaluate这需要两个节点作为参数。
- 我通过发送两个Num来调用此函数。当我使用GetType()。ToSTring()时,它似乎意识到它收到的参数实际上是Nums,而不是节点。

- 我如何演员Nums的参数,所以我可以使用所有成员
函数和变量,而无需手动执行,即(Num)someVariable。我想这样做,所以我可以发送
评估Node的许多不同的孩子,而不是为每次添加进一步复杂化我的代码。我无法弄清楚如何使用
GetType()。

最感谢帮助。
干杯
dave
hi

- I have a class Node, with a child class Num, that inherits from Node.
- I have a function "Evaluate" that takes two Nodes as an argument.
- I call this function by sending it two "Num"''s. When i use
GetType().ToSTring(), it seems to be aware that the arguments it recieved
are in fact Nums, not Nodes.

- How do I cast the arguments to Nums so I can use all the member
functions and variables, without doing
it manually, ie (Num)someVariable. I want to do this so I can send
Evaluate lots of different childs of Node, without having complicate
my code further for each addition. I can''t figure out how to do it with
GetType().

Help most appreciated.
Cheers
dave



嗯这看起来很难。问题是,Num很快将加入由Matrix,Vector,Complex等加入的
。在2组中,那将'

赚了很多If声明!

hmmm looks like this will be a hard one. The problem is, Num will soon be
joined joined by Matrix, Vector, Complex, etc. And in groups of 2, that''ll
make a lot of If statements!


你不能在运行时动态演员。


即使你可以在一个想象的世界,它仍然没有你的好处。

一,在编译时,它不知道你的类型是什么,并且

因此你不能称之为任何方法。二,既然你的节点不同,你就需要一个不同的块来处理每个案例,因此你需要回到你开始的地方。

" David Sobey"写道:
You can''t dynamically cast at run time.

and even if you could in an imaginary world, it would still do you no good.
one, at compile time, it''d have no idea what your type is suppose to be, and
hence you can''t call any methods. two, since your nodes are different, you''d
still need a different block to handle each individual case, and therefore
you are back to where you started.

"David Sobey" wrote:


- 我有一个Node类,有一个子类Num,继承自Node。
- 我有一个函数"评估"这需要两个节点作为参数。
- 我通过发送两个Num来调用此函数。当我使用GetType()。ToSTring()时,它似乎意识到它收到的参数实际上是Nums,而不是节点。

- 我如何演员Nums的参数,所以我可以使用所有成员函数和变量,而无需手动执行,即(Num)someVariable。我想这样做,所以我可以发送评估
许多不同的Node子,而不会使每个添加的代码进一步复杂化。我无法弄清楚如何使用
GetType()。

最感谢帮助。
干杯
dave
hi

- I have a class Node, with a child class Num, that inherits from Node.
- I have a function "Evaluate" that takes two Nodes as an argument.
- I call this function by sending it two "Num"''s. When i use
GetType().ToSTring(), it seems to be aware that the arguments it recieved
are in fact Nums, not Nodes.

- How do I cast the arguments to Nums so I can use all the member functions
and variables, without doing
it manually, ie (Num)someVariable. I want to do this so I can send Evaluate
lots of different childs of Node, without having complicate
my code further for each addition. I can''t figure out how to do it with
GetType().

Help most appreciated.
Cheers
dave



这篇关于使用GetType进行类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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