除了在下面的代码中使用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;
}