1

有没有办法从表记录中解析枚举值。例如,我有一个包含用户数据的类,其中一个是 Enum 类型。数据是从 DataRow 传递的,但我无法解析枚举值。我尝试过这样的事情,

uType= (EType) Enum.TryParse(typeof(row["userType"]));

但它不会编译。任何提示?

谢谢。

4

2 回答 2

1

尝试使用此代码

uType = (EType) Enum.Parse(typeof(EType), row["userType"].ToString(), true);
于 2012-08-18T12:53:40.793 回答
1

Enum.TryParse返回一个布尔值,指示该值是否可以成功解析。

假设这userType是 astring中的DataTable

EType eType;
bool canParse = Enum.TryParse(row.Field<String>("userType"), out eType);
于 2012-08-18T12:57:52.410 回答