C#枚举解析和反思

时间:2012-12-10 21:44:05

标签: c# enums

我的枚举定义如下:

public enum Format {
  Normal = 1,
  Type2 = 2,
  Type3 = 3
}

我正在尝试使用Reflection并调用动态类型转换函数。但是在下面的代码中,“value”的值是“3”而不是“Type3”,并且它不被识别为枚举。是否可以使用int值3来识别枚举?

Type enumType = property.PropertyType;
if (Enum.IsDefined(enumType, value))
   return Enum.Parse(enumType, value);

1 个答案:

答案 0 :(得分:3)

您需要调用Enum.ToObject()将原始值转换为枚举的盒装实例。