关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

C#实现对象转字典

发布时间:2019-11-27 17:17:40

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;

        }

    }

}



/template/Home/Zkeys/PC/Static