I was looking for a way to refactor and simplify one function where I have to do data sorting depending on input class type, and got stuck at switch(input.GetType()):
Quick search led me to Why doesn't C# switch statement allow using typeof/GetType()? with a link to http://blogs.msdn.com/peterhal/archive/2005/07/05/435760.aspx
I read the documentation, but I don't get the justification that the situation is confusing.
From the article:
Unfortunately, like many 'simple'
language features, type switch is not
as simple as it first appears. The
troubles start when you look at a more
significant, and no less important,
example like this:
class C {}
interface I {}
class D : C, I {}
switch typeof(e) {
case C: ... break;
case I: ... break;
default: ... break;
}
What's not simple about it? The call typeof(e)
cannot return - this is a I
D
and C
. It has to return a Type
not an array of interface and class types - Type[]
. So the type of the class D
is D
. And D
corresponds to a default:
branch.
An I missing something?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…