Possible Duplicate:
C# - Is there a better alternative than this to ‘switch on type’?
如果你想在一类物体上点击switch
,最好的方法是什么?
代码片段
private int GetNodeType(NodeDTO node)
{
switch (node.GetType())
{
case typeof(CasusNodeDTO):
return 1;
case typeof(BucketNodeDTO):
return 3;
case typeof(BranchNodeDTO):
return 0;
case typeof(LeafNodeDTO):
return 2;
default:
return -1;
}
}
我知道这样不行,但我想知道你怎么解决这个问题.
或者您是否使用switch 并在类型中添加.ToString()
?