action方法 Register(UserModel userModel)
{
.............................
}
好了废话不多....
/// summary>
/// 反射获取类的属性
/// /summary>
/// param name="type">/param>
/// returns>/returns>
protected PropertyInfo[] GetPropertyInfoArray(Type type)
{
PropertyInfo[] props = null;
try
{
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{
}
return props;
}
/// summary>
/// 根据NameValueCollection 自动装配
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="valueCollection">/param>
/// returns>/returns>
protected T AssembleModelT>(NameValueCollection valueCollection)
{
PropertyInfo[] propertyInfoList = GetPropertyInfoArray(typeof(T));
object obj = Activator.CreateInstance(typeof(T), null);//创建指定类型实例
foreach (string key in valueCollection.Keys)//所有上下文的值
{
foreach (var PropertyInfo in propertyInfoList)//所有实体属性
{
if (key.ToLower() == PropertyInfo.Name.ToLower())
{
PropertyInfo.SetValue(obj, valueCollection[key], null);//给对象赋值
}
}
}
return (T)obj;
}