我有一个问题,无法确定如何在运行时创建枚举的实例。我已经获取了枚举的System.Type并检查了BaseType是否为System.Enum。我的值是一个int值,匹配神秘枚举中的项。
到目前为止,我所拥有的代码仅仅是上述逻辑,如下所示。
if (Type.GetType(type) != null)
{
if (Type.GetType(type).BaseType.ToString() == "System.Enum")
{
return ???;
}
}
在以往使用枚举类型时,我通常在编码时就知道我要解析哪个枚举值。但在当前情况下,我感到困惑,并且在使用谷歌搜索时没有找到有用的答案。我通常会做这样的事情:
When working with Enums in the past i have always know at code time which enum i am trying to parse but in this scenario im confused and have had little luck articulating my question in a google friendly way... I would usually do something like
(SomeEnumType)int
但由于我在编写代码时不知道EnumType,我该如何实现相同的功能?
Type.GetType(type).BaseType.ToString() == "System.Enum"
告诉我对象type
已经是你的SomeEnumType
类型,那么为什么需要将它从SomeEnumType
转换成SomeEnumType
?你能否提供更多背景信息,说明你想要实现什么? - outcoldman