将对象转换成Dictionary 字典
/// <summary> /// /// 将对象属性转换为key-value对 /// </summary> /// <param name="o"></param> /// <returns></returns> public static Dictionary<string, string> ToMap(Object o) { Dictionary<string, string> map = new Dictionary<string, string>(); Type t = o.GetType(); PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo p in pi) { MethodInfo mi = p.GetGetMethod(); if (mi != null && mi.IsPublic) { map.Add(p.Name, mi.Invoke(o, new Object[] { }).ToString()); } } return map; }
这个方法使用的时候传入的对象中的属性不能为null