C# .NET 实体类转Dictionary
--
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
System.Reflection.PropertyInfo[] cfgItemProperties = cfgItem.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); SortedDictionary<string, string> sdCfgItem = new SortedDictionary<string, string>(); foreach (System.Reflection.PropertyInfo item in cfgItemProperties) { string name = item.Name; object value = item.GetValue(cfgItem, null); if (value != null && (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && !string.IsNullOrWhiteSpace(value.ToString())) { sdCfgItem.Add(name, value.ToString()); } }
--
cfgItem 是实体类。
自己可以封装为公共方法。

更多精彩