Type type = typeof(Model.Users).GetProperties()[i].PropertyType; //判断type类型是否为泛型,因为nullable是泛型类, if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类 { //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换 System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type); //将type转换为nullable对的基础基元类型 type = nullableConverter.UnderlyingType; } typeof(Model.Users).GetProperty(typeof(Model.Users).GetProperties()[i].Name).SetValue(u,Convert.ChangeType(dic[item],type), null);
时间: 2024-10-29 06:05:24