zoukankan      html  css  js  c++  java
  • c# 根据自定义Attribute排序

    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;
     
           }










  • 相关阅读:
    封装好的PHP分页类,简单好用--在开源看到的,取回来自己用
    php网站判断用户是否是手机访问的方法
    三种php连接access数据库方法
    php防止SQL注入详解及防范
    mysql sql语句大全
    java util 中set,List 和Map的使用
    web开发——写一个简单的表格导出操作
    JSP登录页面使用Enter键登录【转】
    PL/SQL 将旧表的一些字段赋值给新的表中的字段的做法
    PL/SQL设置主键自增
  • 原文地址:https://www.cnblogs.com/facial/p/5183829.html
Copyright © 2011-2022 走看看