c#获取特性DescriptionAttribute的值

int detailId = Convert.ToInt32(id);
                BillLoanApplyDetail model = _billLoadApplyDetail.GetBillLoanApplyDetailById(detailId);
                Dictionary<string, string> dictList = new Dictionary<string, string>();
                if (model != null)
                {

                    PropertyInfo[] ps = model.GetType().GetProperties();

                    foreach (PropertyInfo info in ps)
                    {
                        if (info.Name.ToLower() != "id")
                        {
                            string descriptionName = ((DescriptionAttribute)Attribute.GetCustomAttribute(info, typeof(DescriptionAttribute))).Description;// 属性值

                            var infoValue = info.GetValue(model);
                            dictList.Add(descriptionName, infoValue!=null?info.GetValue(model).ToString():string.Empty);    
                        }
                        
                    }

                    return Json(dictList, JsonRequestBehavior.AllowGet);
                    
                    
                }
                else
                {
                    return Json("没有详情",JsonRequestBehavior.AllowGet);
                }

c#获取特性DescriptionAttribute的值_第1张图片

你可能感兴趣的:(attribute)