/// <summary>对象转换为字典
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static Dictionary<string, object> ToDictionary<T>(this T obj) where T : class
{
var dictionary = new Dictionary<string,object>();
if (obj != null)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
{
dictionary.Add(descriptor.Name.Replace('_', '-'), descriptor.GetValue(obj));
}
}
return dictionary;
}
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static Dictionary<string, object> ToDictionary<T>(this T obj) where T : class
{
var dictionary = new Dictionary<string,object>();
if (obj != null)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
{
dictionary.Add(descriptor.Name.Replace('_', '-'), descriptor.GetValue(obj));
}
}
return dictionary;
}