我需要转换类型泛型的值..但我需要获得转换属性的类型...我怎么能做到这一点?获取属性类型和转换通用
public static T ConvertToClass<T>(this Dictionary<string, string> model)
{
Type type = typeof(T);
var obj = Activator.CreateInstance(type);
foreach (var item in model)
{
type.GetProperty(item.Key).SetValue(obj, item.Value.DynamicType</*TYPE OF PROPERTY*/>());
}
return (T)obj;
}
public static T DynamicType<T>(this string value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
你应该使用Json.Net来做到这一点。看到这里: http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net – Brannon 2014-10-03 18:27:06