遍历一个类的属性--并转换为Dictionary类型

参考地址...http://www.cnblogs.com/xwgli/p/3306297.html

记录点滴...以前很少用泛型...HaHa...
        /// <summary>
        /// 遍历一个类--并将类的属性遍历
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public void GetProperToDic<T>(T entity) where T : class
        {

            PropertyInfo[] properArr = entity.GetType().GetProperties();
            foreach (var vv in properArr)
            {
                Console.Write("{0},{1}", vv.Name, vv.GetValue(entity));
            }
            //下面是关于ToDictionary的用法...
            Dictionary<string, object> retDic = properArr.ToDictionary(k => k.Name, val => val.GetValue(entity));
            foreach (KeyValuePair<string, object> v in retDic)
            {
                Console.Write("{0},{1}", v.Key, v.Value);
            }

        }

....

原文地址:https://www.cnblogs.com/love-zf/p/5672690.html