C# 设置及获取属性备注信息 Description

C# 设置及获取属性备注信息 Description

1.设置

using System.ComponentModel;

public class List_ParamsModel

{

[Description("开始时间")] [Description("结束时间")]
public string searchDateEnd { get; set; }

2.获取

using System.Reflection;

using System.ComponentModel;

 

       /// <summary>         /// </summary>         /// <returns></returns>         {             //Type type = typeof(List_ParamsModel);//model.GetType()             System.Reflection.PropertyInfo[] properties = model.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);             {             }             {                 {                     object value = property.GetValue(model, null);  //值                     object[] objs = property.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);

                    if (value != null && !string.IsNullOrEmpty(value.ToString()) && objs.Length > 0 && ((System.ComponentModel.DescriptionAttribute)objs[0]).Description != null)                         des = ((System.ComponentModel.DescriptionAttribute)objs[0]).Description;                     }             }         }

posted @ 2021-01-05 13:05  不要摸我的腰  阅读(2281)  评论(0编辑  收藏  举报