实体类的属性转换成Dictionary

        private static Dictionary<string, string> ConvertToDic(StationModel data)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (PropertyInfo pro in typeof(StationModel).GetProperties())
            {
                string value = pro.GetValue(data, null)?.ToString();
                string name = pro.Name;
                if (string.IsNullOrEmpty(value)) continue;
                if (name == "Type") continue;
                dic.Add(name, value);
            }
            return dic;
        }

如果属性字段都是string类型就用Dictionary<string, string>,否则可以用Dictionary<string, object>

posted @ 2017-07-28 09:33  花生打代码会头痛  阅读(1463)  评论(0)    收藏  举报