C#获取对象实体的键值对信息
/// <summary> /// 键值对 /// </summary> public class Kv { /// <summary> /// 键 /// </summary> public string Key { get; set; } /// <summary> /// 值 /// </summary> public string Value { get; set; } }
List<Kv> kvs = new List<Kv>(); DataCircular circular = new DataCircular(); // 获取DataCircular实体的所有属性键和值 Type type = circular.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { Kv _kv = new Kv(); _kv.Key = property.Name; if (property.GetValue(data) is double) { _kv.Value = ((float)Convert.ToDouble(property.GetValue(data))).ToString("f2"); } else { _kv.Value = property.GetValue(data).ToString(); } kvs.Add(_kv); }