public static T TableRowToModel<T>(T objmodel, System.Collections.Specialized.NameValueCollection form)
{
foreach (PropertyInfo info in typeof(T).GetProperties())
{
string name = info.Name;
if (form.GetValues(name) != null)
{
//如果不是泛型
if (!info.PropertyType.IsGenericType)
{
//如果是空则设置空,非空则设置值。
info.SetValue(objmodel, string.IsNullOrEmpty(form.GetValues(name).ToString()) ? null : Convert.ChangeType(form.GetValues(name), info.PropertyType), null);
}
//如果是泛型,则找他的基础类型
else if (info.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
info.SetValue(objmodel, string.IsNullOrEmpty(form.GetValues(name).ToString()) ? null : Convert.ChangeType(form.GetValues(name), Nullable.GetUnderlyingType(info.PropertyType)), null);
}
}
}
return objmodel;
}
再ASP.NET中快速的把Request.Form 转换成一个model
最新推荐文章于 2022-01-10 21:26:06 发布