public static T ToDto <T>(this JToken data) where T : new() { var tempResult = Activator.CreateInstance <T>(); ////创建指定类型的实例 var propertypes = tempResult.GetType().GetProperties(); //得到类的属性 if (data != null) { foreach (PropertyInfo pro in propertypes) { var tempName = pro.Name.ToLower(); var value = data[pro.Name] != null ? data[pro.Name] : data[tempName]; var setDelegate = DynamicMethodFactory.CreatePropertySetter(pro); if (value == null) { setDelegate(tempResult, GetDefaultValue(pro.PropertyType)); } else { setDelegate(tempResult, GetValue(pro, value)); } } } else { tempResult = data.Deserialize <T>(); } return(tempResult); }
public static T DeserializeToDto <T>(this JToken data) where T : new() { var tempResult = Activator.CreateInstance <T>(); ////创建指定类型的实例 if (data != null) { tempResult = data.Deserialize <T>(); } return(tempResult); }