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










相关文章:

  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
猜你喜欢
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2021-08-29
  • 2021-07-07
  • 2021-06-27
相关资源
相似解决方案