您不能使用switch块来测试type的值
Type。编译代码会给您一个错误,例如:
开关表达式或大小写标签必须是bool,char,string,integral,enum或相应的可为空的类型
您将需要使用
if-
else语句。
另外:
typeof(int)和
typeof(Int32)是等效的。
int是关键字,
Int32是类型名称。
更新
如果您期望大多数类型都是固有类型,则可以通过将开关块与一起使用来提高性能
Type.GetTypeCode(...)。
例如:
switch (Type.GetTypeCode(type)){ case TypeCode.Int32: // It's an int break; case TypeCode.String: // It's a string break; // Other type pre cases here... default: // Fallback to using if-else statements... if (type == typeof(MyCoolType)) { // ... } else if (type == typeof(MyOtherType)) { // ... } // etc...}

![如何在Type上使用开关盒?[重复] 如何在Type上使用开关盒?[重复]](http://www.mshxw.com/aiimages/31/372716.png)
