获取实体类里面所有的名称、DESCRIPTION值
//获取实体类里面所有的名称、DESCRIPTION值 public Dictionary<string, string> GetDescription<T>(T t) { Dictionary<string, string> dic = new Dictionary<string, string>(); PropertyInfo[] ps = t.GetType().GetProperties(); foreach (var item in ps) { if (item.CustomAttributes.Where(p => p.AttributeType.Name.Contains("DescriptionAttribute")).FirstOrDefault() != null) { var des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description; dic.Add(item.Name, des); } } return dic; } //获取实体类里面所有的名称、值、DESCRIPTION值 public string GetProperties<T>(T t) { string tStr = string.Empty; if (t == null) { return tStr; } PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); if (properties.Length <= 0) { return tStr; } foreach (PropertyInfo item in properties) { string name = item.Name; //名称 object value = item.GetValue(t, null); //值 string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description;// 属性值 if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { tStr += string.Format("{0}:{1}:{2},", name, value, des); } else { GetProperties(value); } } return tStr; }