而不是使用如下代码所示的Switch语句,是否有其他方法来检查foo.Type
是否与Parent.Child
类中的任何常量匹配?如何通过静态类常量进行循环?
预期的目标是循环遍历所有常量值以查看foo.Type
是否匹配,而不必将每个常量指定为case
。
父类:
public class Parent
{
public static class Child
{
public const string JOHN = "John";
public const string MARY = "Mary";
public const string JANE = "Jane";
}
}
代码:
switch (foo.Type)
{
case Parent.Child.JOHN:
case Parent.Child.MARY:
case Parent.Child.JANE:
// Do Something
break;
}
可以定义阵列中的全部通过数组常量和环路检查常数值? – Peter
您可以将所有常量值放入ArrayList中,在比较特定元素是否等于foo.Type时迭代列表。 – HaroldSer
这可能有助于http://stackoverflow.com/questions/14971631/convert-an-enum-to-liststring,然后只是做一个** IndexOf(foo.Type)** –