s
o
u
l
s
j
i
e

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);
}

 

posted @ 2023-05-24 09:35  soulsjie  阅读(189)  评论(0编辑  收藏  举报
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43