c# 根据自定义Attribute排序
2016-02-06 09:04
609 查看
add a class: public class ExportAttribute : Attribute { public int FieldOrder { get; set; } public ExportAttribute() { } } add [ExportAttribute(FieldOrder = 2)] on the Field
[DisplayName("Department Name")] [ExportAttribute(FieldOrder = 2)] public string DepartmentName { get { return this.Department.DepartmentName; } } Get the class's filed those have the DisplayName Attribute and order by the FieldOrder DisplayName
public PropertyInfo[] GetPropertyInfoArray(Type type) { PropertyInfo[] props = null; try { object obj = Activator.CreateInstance(type); //props = (from r in type.GetProperties(BindingFlags.Public | BindingFlags.Instance) // where r.GetCustomAttribute(typeof(DisplayNameAttribute)) != null // select r).ToArray(); props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(x => new { Property = x, Attribute = (ExportAttribute)Attribute.GetCustomAttribute(x, typeof(ExportAttribute), true) }) .Where(x => x.Property.GetCustomAttribute(typeof(DisplayNameAttribute)) != null ) .OrderBy(x => x.Attribute != null ? x.Attribute.FieldOrder : -1) .Select(x => x.Property ) .ToArray(); } catch (Exception ex) { AppLogger.LogErrorOnly(ex); } return props; }
相关文章推荐
- C#文本框中默认是不允许使用全选的
- C# 二叉查找树实现
- C# 基础加强(二) 编译执行过程器
- C# 基础加强(一)可变参数params、索引器
- c#之new关键字
- C#操作字符串方法总结<转>
- C# DataTable 和List之间相互转换的方法
- C# 的List<T>筛选数据填充到另一个List<T>
- c#编程之UDP通信
- C#格式化数值结果表
- crc校验码生成程序(C#)
- C# 串口通信程序
- C#调用VC DLL接口函数参数类型对应介绍
- c#——完美实现短信验证
- C#寒假学习笔记(2)
- C#集合
- C#成神之路<12> C#数组
- C# 中 DataTable 使用详解。
- c#导出到excel的几种解决方案
- c#之密封类