public static T ToObject <T>(Dictionary <string, object> dict, T obj) where T : class { if (dict == null) { return(null); } Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); //Properties Map for (int i = 0; i < properties.Length; i++) { PropertyInfo p = properties[i]; var propertyType = p.PropertyType; NameInMapAttribute attribute = p.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? p.Name : attribute.Name; if (dict.ContainsKey(realName)) { var value = dict[realName]; if (value == null) { p.SetValue(obj, value); continue; } p.SetValue(obj, MapObj(propertyType, value)); } } return(obj); }
public static Dictionary <string, object> ToMap(this TeaModel model) { if (model == null) { return(null); } var result = new Dictionary <string, object>(); Type type = model.GetType(); PropertyInfo[] properties = type.GetProperties(); //PropertyInfo for (int i = 0; i < properties.Length; i++) { PropertyInfo propertyInfo = properties[i]; Type property = propertyInfo.PropertyType; if (typeof(Stream).IsAssignableFrom(property)) { continue; } NameInMapAttribute attribute = propertyInfo.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? propertyInfo.Name : attribute.Name; result.Add(realName, ToMapFactory(property, propertyInfo.GetValue(model))); } return(result); }