直接上代码:把一个类转换成object,然后在转换成字典
internal static IDictionary<string, string> GetDictionary(this object source)
{
if (source == null)
{
return new Dictionary<string, string>();
}
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(source);
Dictionary<string, string> dictionary = new Dictionary<string, string>();
for (int i = ; i < properties.Count; i++)
{
dictionary.Add(properties[i].Name, properties[i].GetValue(source).ToString());
}
return dictionary;
}
htmlAttributes是一个objuect类型(一个匿名类new {name="张三"})
调用: IDictionary<string, string> dictionary = this.htmlAttributes.GetDictionary();