private static Boolean SetControlValue(Control control, Object value) { TypeX tx = control.GetType(); String name = tx.GetCustomAttributeValue <ControlValuePropertyAttribute, String>(); PropertyInfoX pix = null; if (!String.IsNullOrEmpty(name)) { pix = PropertyInfoX.Create(tx.Type, name); } if (pix == null) { pix = PropertyInfoX.Create(tx.Type, "Value"); } if (pix == null) { pix = PropertyInfoX.Create(tx.Type, "Text"); } if (pix != null) { if (value == null && pix.Type.IsValueType) { return(false); } pix.SetValue(control, value); return(true); } return(false); }
/// <summary> /// 将对象赋值 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="rval">表单集合字符串</param> /// <param name="model">从数据库查询的实体对象</param> /// <returns></returns> public static T ConvertToModel <T>(this string rval, T model) { Type modelType = model.GetType(); PropertyInfo[] props = modelType.GetProperties();//获取此对象的,字段集合 object propertValue = null; string value = String.Empty; foreach (PropertyInfo item in props) { if (rval.ValidationKey(item.Name)) { value = rval.GetFromValue(item.Name); propertValue = GetVal(propertValue, value, item.PropertyType.Name); if (item.PropertyType.Name == "Double")//如果对象类型是Double数字,等于0则不予赋值 { if (propertValue.ToString() != "0") { PropertyInfoX.SetValue(model, item.Name, propertValue);//给字段赋值 } } else { PropertyInfoX.SetValue(model, item.Name, propertValue);//给字段赋值 } } } return(model); }
public IEnumerable <T> BindModel <T>(object source) { DataTable dt = (DataTable)source; IEnumerable <DataColumn> dclist = dt.Columns.Cast <DataColumn>(); List <T> modellist = new List <T>(); foreach (DataRow dr in dt.Rows) { TypeX typex = TypeX.Create(typeof(T)); T modelInstance = (T)typex.CreateInstance(); //T modelInstance = (T)Activator.CreateInstance(typeof(T)); foreach (PropertyInfo property in typeof(T).GetProperties()) { DataColumn dc = dclist.Where(item => string.Compare(property.Name, item.ColumnName, true) == 0).FirstOrDefault(); if (null != dc) { PropertyInfoX propx = PropertyInfoX.Create(property); propx.SetValue(modelInstance, dr[dc.ColumnName]); //property.SetValue(modelInstance, Convert.ChangeType(dr[dc.ColumnName], property.PropertyType),null); } } modellist.Add(modelInstance); } return(modellist); }
/// <summary> /// [OLD Code]返回一个新创建的实体对象,并给各个字段赋值 /// </summary> /// <param name="typeName">完整实体名称, [命名空间.实体名称]</param> /// <param name="rval">表单集合字符串</param> /// <returns></returns> public static object GetFormEntity(this string rval, string typeName) { typeName = string.Format("{0}.{1}", ConfigHelper.GetAppSettings("EntitySpaceName"), typeName); TypeX EntityType = TypeX.Create(TypeX.GetType(typeName, true)); //根据类的完整名称建立类的类型,用于动态创建类 如: Clump.Mobile.Models.NewsInfo Object objEntity = EntityType.CreateInstance(); //动态建立类的对象 实体类的对象Object objEntity = EntityType.CreateInstance(true);意思是在创建实体对象时 A a = new A(true) PropertyInfo[] props = objEntity.GetType().GetProperties(); //获取此对象的,字段集合 object propertValue = String.Empty; foreach (PropertyInfo item in props) { propertValue = rval.GetFromValue(item.Name); if (!"".Equals(propertValue)) { //根据字段类型,转换格式 switch ((PropertyInfoX.Create(EntityType, item.Name)).Property.PropertyType.Name) { case "String": propertValue = Convert.ToString(propertValue); break; case "Int64": propertValue = Convert.ToInt64(propertValue); break; case "Int32": propertValue = Convert.ToInt32(propertValue); break; case "Int16": propertValue = Convert.ToInt16(propertValue); break; case "Byte": propertValue = Convert.ToByte(propertValue); break; case "Single": propertValue = Convert.ToSingle(propertValue); break; case "Double": propertValue = Convert.ToDouble(propertValue); break; case "Boolean": propertValue = Convert.ToBoolean(propertValue); break; case "DateTime": propertValue = Convert.ToDateTime(propertValue); break; default: break; } PropertyInfoX.SetValue(objEntity, item.Name, propertValue);//给字段赋值 } } PropertyInfoX.SetValue(objEntity, "CreateTime", DateTime.Now);//给CreateTime字段赋值,添加时间字段是每个数据表标配(未解决:MySql配置,暂时不能默认添加当前时间) return(objEntity); }
/// <summary> /// 将对象赋值 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="formModel">表单获取的实体对象</param> /// <param name="model">从数据库查询的实体对象</param> /// <returns></returns> public static T ConvertToModel <T>(T formModel, T model) { Type formModelType = formModel.GetType(); Type modelType = model.GetType(); PropertyInfo[] props = modelType.GetProperties(); //获取此对象的,字段集合 PropertyInfo[] formProps = formModelType.GetProperties(); //获取此对象的,字段集合 object propertValue = null; string value = String.Empty; for (int i = 0; i < props.Length; i++) { if (props[i].Name.Equals(formProps[i].Name) && props[i].CanWrite == true) { propertValue = PropertyInfoX.GetValue(formModel, formProps[i].Name); if (propertValue != null) { PropertyInfoX.SetValue(model, props[i].Name, propertValue);//给字段赋值 } } } return(model); }
/// <summary> /// 把实体成员的值设置到控件上 /// </summary> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetFormItem(FieldItem field, System.Web.UI.Control control, Boolean canSave) { if (field == null || control == null) { return; } String toolTip = String.IsNullOrEmpty(field.Description) ? field.Name : field.Description; if (field.IsNullable) { toolTip = String.Format("请填写{0}!", toolTip); } else { toolTip = String.Format("必须填写{0}!", toolTip); } if (control is Label) { toolTip = null; } if (control is ExtAspNet.ControlBase) { ExtAspNet.ControlBase wc = control as ExtAspNet.ControlBase; // 设置ToolTip //if (String.IsNullOrEmpty(wc.ToolTip) && !String.IsNullOrEmpty(toolTip)) wc.ToolTip = toolTip; //// 必填项 //if (!field.IsNullable) SetNotAllowNull(field, control, canSave); // 设置只读,只有不能保存时才设置,因为一般都不是只读,而前端可能自己设置了控件为只读,这种情况下这里再设置就会修改前端的意思 if (!canSave) { if (wc is ExtAspNet.TextBox) { (wc as TextBox).Enabled = !canSave; } else { wc.Enabled = canSave; } } // 分控件处理 if (wc is TextBox || wc is DatePicker) { SetFormItemTextBox(field, wc as RealTextField, canSave); } else if (wc is Label) { SetFormItemLabel(field, wc as Label, canSave); } else if (wc is RadioButton) { SetFormItemRadioButton(field, wc as RadioButton, canSave); } else if (wc is CheckBox) { SetFormItemCheckBox(field, wc as CheckBox, canSave); } else if (wc is DropDownList) { SetFormItemListControl(field, wc as DropDownList, canSave); } else { SetControlValue(control as ControlBase, Entity[field.Name]); } } else { SetControlValue(control as ControlBase, Entity[field.Name]); PropertyInfoX pix = PropertyInfoX.Create(control.GetType(), "ToolTip"); if (pix != null && String.IsNullOrEmpty((String)pix.GetValue(control))) { pix.SetValue(control, toolTip); } } }