关于枚举取值

小白_YY 2009-12-08 03:44:44
public enum HandleInd
{
[Description("已处理")]
Processed = 'Y',
[Description("未处理")]
Untreated = 'N'
}

怎么获取Description 和 值 'Y'
...全文
59 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ProjectDD 2009-12-09
  • 打赏
  • 举报
回复
在这个上面查

typeof(HandleInd)
ProjectDD 2009-12-09
  • 打赏
  • 举报
回复
enum都是 默认继承int类型的
你对它赋值一个char那么它会自动的把它二转化为一个int值

要取Discrebe的值 的话需要 在.GetType().XXX 上查
xiaoyuzi 2009-12-09
  • 打赏
  • 举报
回复
取描述值
private void button1_Click(object sender, EventArgs e)
{
HandleInd hi = HandleInd.Processed;
MessageBox.Show(GetDescription(hi));
HandleInd hi2 = HandleInd.Untreated;
MessageBox.Show(GetDescription(hi2));
}

private string GetDescription(Enum en)
{
Type type = en.GetType();
System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
zr2199 2009-12-08
  • 打赏
  • 举报
回复
HandleInd.Processed 或者HandleInd[0]都可以吧
小白_YY 2009-12-08
  • 打赏
  • 举报
回复
Enum.Parse()
Enum.GetValues()
取的不 是'Y'

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧