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










  • 相关阅读:
    Springboot使用slf4j记录日志和lombok(能用的1)-主要看这个!
    mysql-覆盖索引(转载)(收藏过)
    Github上开源仿京东商城项目启动配置详解(小白版)
    java8 stream常用用法(转载)
    JDK下载过慢的问题解决方案
    intellij idea 的全局搜索快捷键方法(转载)
    Linux终端复制粘贴快捷命令
    Kali入门配置
    Google搜索
    Dig
  • 原文地址:https://www.cnblogs.com/facial/p/5183829.html
Copyright © 2011-2022 走看看