【发布时间】:2011-09-25 00:16:43
【问题描述】:
可能重复:
Is there a better alternative than this to 'switch on type'?
我需要遍历我的类的所有属性并检查它的 int 类型是否我需要做某事,如果它的字符串 .. 则做某事。我需要它使用开关盒。在这里,我以下列方式使用 switch,但它要求一些常数。看下面的代码:
public static bool ValidateProperties(object o)
{
if(o !=null)
{
var sourceType = o.GetType();
var properties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (var property in properties)
{
var type = property.GetType();
switch (type)
{
*case typeof(int):* getting error here
// d
}
}
}
}
我也想知道,我应该使用什么检查,typeof(int) 还是 typeof(Int32)?
【问题讨论】:
-
仅供参考,这两个问题的上下文不同。他在问控件,而我在问数据类型。请在任何反对或接近投票之前确定。不要盲目。有这种名声的人不应该犯这种幼稚的错误。
标签: c#