/// <summary> /// 为Entity赋值 /// </summary> /// <typeparam name="T">Entity</typeparam> /// <param name="t">Entity</param> /// <param name="form">Request</param> /// <param name="prefix">前缀</param> public static void UpdateModel <T>(T t, NameValueCollection form, string prefix) where T : Entity { Field[] fields = EntityCache.GetFields <T>(); string temp = prefix ?? string.Empty; foreach (Field f in fields) { string tempValue = form[temp + f.PropertyName]; if (null != tempValue) { string propertyName = f.PropertyName; PropertyInfo property = t.GetType().GetProperty(propertyName); if (null == property) { propertyName = string.Concat(propertyName.Substring(0, 1).ToUpper(), propertyName.Substring(1)); property = t.GetType().GetProperty(propertyName); } if (null != property) { DataUtils.SetPropertyValue(t, property, tempValue); } } } }
/// <summary> /// 为Entity赋值 /// </summary> /// <typeparam name="T">Entity</typeparam> /// <param name="t">Entity</param> /// <param name="form">Request</param> /// <param name="prefix">前缀</param> /// <returns>转换是否成功</returns> public static bool TryUpdateModel <T>(T t, NameValueCollection form, string prefix) { bool success = true; Field[] fields = EntityCache.GetFields <T>(); string temp = prefix ?? string.Empty; foreach (Field f in fields) { string tempValue = form[temp + f.PropertyName]; if (null != tempValue) { try { string propertyName = f.PropertyName; PropertyInfo property = t.GetType().GetProperty(propertyName); if (null == property) { propertyName = string.Concat(propertyName.Substring(0, 1).ToUpper(), propertyName.Substring(1)); property = t.GetType().GetProperty(propertyName); } if (null != property) { DataUtils.SetPropertyValue(t, property, tempValue); } } catch { success = false; } } } return(success); }
/// <summary> /// 根据字段名返回实体字段 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="fieldName"></param> /// <returns>返回null 就表示字段不存在</returns> public static Field GetField <TEntity>(string fieldName) where TEntity : Entity { Field[] fields = EntityCache.GetFields <TEntity>(); foreach (Field field in fields) { if (string.Compare(fieldName, field.PropertyName, true) == 0) { return(field); } } return(null); }