代码对象转文字描述,适合分类,类别对象
2012-02-09 15:34
525 查看
直接上代码:
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Text;
namespace MvcApp.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
Type type = typeof(DescriptionAttribute);
StringBuilder str = new StringBuilder();
str.AppendLine("代码对象转文字描述:");
foreach (FieldInfo fi in typeof(CustomerRankType).GetFields())
{
object[] arr = fi.GetCustomAttributes(type, true);
if (arr.Length > 0)
{
str.AppendLine(((DescriptionAttribute)arr[0]).Description);
// nvc.Add(fi.Name, ((DescriptionAttribute)arr[0]).Description);
}
}
ViewData["Message"] = str.ToString();
return View();
}
public ActionResult About()
{
return View();
}
}
/// <summary>
/// 用户等级枚举
/// </summary>
[Serializable]
public enum CustomerRankType
{
[Description("游客")]
Unknown = 0,
[Description("初级会员")]
JuniorMember = 1,
[Description("青铜会员")]
BronzeMember = 2,
[Description("白银会员")]
SilverMember = 3,
[Description("黄金会员")]
GoldMember = 4,
[Description("钻石会员")]
DiamondMember = 5,
[Description("皇冠会员")]
CrownMember = 6,
[Description("至尊会员")]
EggEmperor = 7
}
}
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Text;
namespace MvcApp.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
Type type = typeof(DescriptionAttribute);
StringBuilder str = new StringBuilder();
str.AppendLine("代码对象转文字描述:");
foreach (FieldInfo fi in typeof(CustomerRankType).GetFields())
{
object[] arr = fi.GetCustomAttributes(type, true);
if (arr.Length > 0)
{
str.AppendLine(((DescriptionAttribute)arr[0]).Description);
// nvc.Add(fi.Name, ((DescriptionAttribute)arr[0]).Description);
}
}
ViewData["Message"] = str.ToString();
return View();
}
public ActionResult About()
{
return View();
}
}
/// <summary>
/// 用户等级枚举
/// </summary>
[Serializable]
public enum CustomerRankType
{
[Description("游客")]
Unknown = 0,
[Description("初级会员")]
JuniorMember = 1,
[Description("青铜会员")]
BronzeMember = 2,
[Description("白银会员")]
SilverMember = 3,
[Description("黄金会员")]
GoldMember = 4,
[Description("钻石会员")]
DiamondMember = 5,
[Description("皇冠会员")]
CrownMember = 6,
[Description("至尊会员")]
EggEmperor = 7
}
}
相关文章推荐
- 《机器学习实战》代码记录--knn--约会对象分类
- WordPress去掉分类链接的描述文字
- SQLServerToSQLite 程序代码的分析翻译及学习(二、根据数据库描述对象创建SQLite数据库)
- 代码中动态设置控件的属性(侧重讲布局位置的动态设置,能直接用控件对象设置的属性如.setText这些不在下面描述)
- python学习 三 01 再爬一个网站,几行代码,搞定分类类别
- postgresql代码研究的主要目录分类描述
- 点击某个按钮切换图片、文字描述(按钮颜色跟着改变)(简化代码)
- postgresql代码研究的主要目录分类描述
- 无限分类添加新类别代码
- 【适合初学者】简单代码讲述json数据与oc对象互相转化
- 无限分类添加新类别代码
- Keras 多层感知机 多类别的 softmax 分类模型代码
- 无限分类添加新类别代码
- Pandas中兼并数组和字典功能的Series 2013-03-24 11:24:00 分类: Python/Ruby In [2]: # 这段代码用于并排显示多个Series对象 from it
- 强大的UITextView分类,自动计算年龄和星座的DatePicker,文字闪动渐变的标签, 一行代码画一个表格
- java代码实现商品类别的无限级分类显示
- 12306支付出错:交易失败!代码:50050描述:交易信息不完整
- 不安全的代码: 教你“随心所欲”地在内存中操作Java的类和对象(2)
- 领域驱动设计之代码优先-架构描述 (翻译)
- (学习笔记)在ActionScript代码中重置对象的属性和样式