首页 技术 正文
技术 2022年11月11日
0 收藏 571 点赞 2,638 浏览 1252 个字

不知不觉在公司一个多月了,这一个月做了一个支票申请的web页面功能,都不是特别难,审核有公司给的工作流,分页工具和很多公用工具公司也都给了,所以觉得难度都不是很大。今天主管让我们修改了以前做的项目的代码规范,有一个问题是html的select标签绑定数据问题,不能用之前写死的,而是要求根据枚举然后动态给select绑定数据,这是我在公司带的遇到的第二个大问题了。

枚举的基础知识网上都有,就不说了,我想说一下枚举除了名称和值以外还一个特性,那就是描述,而一般值是int,名称是英文,描述可以是中文作为select的option显示,定义如下:

public enum Url{
[Description("http://www.thylx.net")]
个人博客 = ,
[Description("http://blog.163.com/thylx133@126/")]
网易博客 = ,
[Description("http://www.8eshare.com/")]
八邑分享 =
}

然后就是获取C#枚举中的描述值的方法了:

  /// <summary>
/// 获取描述信息
/// </summary>
/// <param name="en">枚举</param>
/// <returns></returns>
public static string GetEnumDes(Enum en) {
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > )
{
object[] attrs = memInfo[].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (attrs != null && attrs.Length > )
return ((DescriptionAttribute)attrs[]).Description;
}
return en.ToString();
}

这些都是从网上po来的,谢谢那位大神,这个写得很清楚了,然后再附上一些enum转int,int转enum,string转enum的方法:

Colors color = (Colors)2              int-》enum(个人觉得这个方法很好用)

(int)Colors.Red                           enum-》int

(Colors)Enum.Parse(typeof(Colors), “Red”)                    string-》enum

Enum.GetName(typeof(Colors),3))                                enum-》string

还有一些enum常用的方法,GetValues和GetName(这个可以查看msdn帮助文档)。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,498
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,911
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,745
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,501
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,139
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,303