环境:net framework 4.5.2


    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading.Tasks;
    7. namespace DrwmePMS.Model {
    8. public class ReportProgress {
    9. [Description("项目名称")]
    10. public string ProjectName { get; set; }
    11. [Description("需求内容")]
    12. public string NeedTitle { get; set; }
    13. [Description("当前状态")]
    14. public string NeedsStage { get; set; }
    15. [Description("人员")]
    16. public string Person { get; set; }
    17. [Description("计划日期")]
    18. public DateTime? DatePlan { get; set; }
    19. [Description("实际交付日期")]
    20. public DateTime? DateComplete { get; set; }
    21. [Description("逾期天数")]
    22. public int? OverdueDay { get; set; }
    23. [Description("交付次数")]
    24. public int? Count { get; set; }
    25. }
    26. }
    1. Model.ReportProgress progress = new Model.ReportProgress();
    2. foreach (var item in progress.GetType().GetProperties()) {
    3. //获得属性指定类型得特征,不写得化返回太多了需要做多余得操作
    4. var attr = item.GetCustomAttributes(typeof(DescriptionAttribute),false);
    5. if (attr is DescriptionAttribute[] a) {
    6. var str1 = a[0].Description;
    7. }
    8. }

    效果图