C#实现对象转字典
在学习C#开发过程中,遇到了要把对象转化为字典,在这里记录一下,方便以后查看。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Application
{
class MapUtils
{
/// <summary>
/// 对象转换为字典
/// </summary>
/// <param name="obj">待转化的对象</param>
/// <param name="isIgnoreNull">是否忽略NULL 这里我不需要转化NULL的值,正常使用可以不穿参数 默认全转换</param>
/// <returns></returns>
public static Dictionary<string, object> ObjectToMap(object obj, bool isIgnoreNull = false)
{
Dictionary<string, object> map = new Dictionary<string, object>();
Type t = obj.GetType(); // 获取对象对应的类, 对应的类型
PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); // 获取当前type公共属性
foreach (PropertyInfo p in pi)
{
MethodInfo m = p.GetGetMethod();
if (m != null && m.IsPublic)
{
// 进行判NULL处理
if (m.Invoke(obj, new object[] { }) != null || !isIgnoreNull) {
map.Add(p.Name, m.Invoke(obj, new object[] { })); // 向字典添加元素
}
}
}
return map;
}
}
}