对于VS2005中的C#,有没有办法检查一个整数是否是Enum类型的一部分?VS2005中的C#:你能检查一个整数是否在一个给定的枚举类型中声明?
如:
if number in CustomerType { ... }
其中
enum CustomerType
{
A = 0;
B = 1;
C = 2;
}
对于VS2005中的C#,有没有办法检查一个整数是否是Enum类型的一部分?VS2005中的C#:你能检查一个整数是否在一个给定的枚举类型中声明?
如:
if number in CustomerType { ... }
其中
enum CustomerType
{
A = 0;
B = 1;
C = 2;
}
是Enum.IsDefined(Type enumType, Object value)你在找什么?
而不是你的if语句:
if (Enum.IsDefined(typeof(CustomerType), number))
{
...
}
尝试是这样的:
var value = Enum.GetName(typeof(CustomerType), 3); // instead of 3 you can use any value
其中CustomerType
是:
public enum CustomerType
{
A = 0,
B = 1,
C = 2,
}
通过传递3价值将有一个空值。如果你传递一个现有值(即0,1或2),那么你会得到“A”,“B”或“C”。
欲了解更多信息,你可以检查System.Enum
类的静态方法。
Regards ...