Bor*_*ens 28 c# switch-statement
我怎样才能做到这一点?
switch(property.PropertyType){
case typeof(Boolean):
//doStuff
break;
case typeof(String):
//doOtherStuff
break;
default: break;
}
Run Code Online (Sandbox Code Playgroud)
我不想使用这个名字,因为字符串比较类型很糟糕,可能会有所变化.
Jor*_*ira 46
System.Type propertyType = typeof(Boolean);
System.TypeCode typeCode = Type.GetTypeCode(propertyType);
switch (typeCode)
{
case TypeCode.Boolean:
//doStuff
break;
case TypeCode.String:
//doOtherStuff
break;
default: break;
}
Run Code Online (Sandbox Code Playgroud)
您可以对TypeCode.Object使用混合方法,使用typeof进行动态处理.这非常快,因为对于第一部分 - 交换机 - 编译器可以基于查找表来决定.
归档时间: |
|
查看次数: |
15029 次 |
最近记录: |