using Core.Mes.Client.Comm.Control; using Core.Mes.Client.Comm.Tool; using CoreFS.SA06; using Infragistics.Win.UltraWinGrid; using System; using System.Reflection; using System.Windows.Forms; //Create By Cx 2015年1月17日 14:12:40 namespace Core.StlMes.Client.Qcm { /// /// 通用实体类转换器(前提条件:实体类设置了转换器所需要的特性) /// /// class FrmDtConverter { /// /// UltraGridRow转换为实体类,并验证是否为空。所能识别的特性:FieldAttribute, FieldAttributeType, NullableAttribute, CoreUserInfo。 /// /// /// /// public Tobj Convert(UltraGridRow row, CoreUserInfo userInfo, FieldAttributeType fieldAttributeType) { Type type = typeof(Tobj); Tobj obj = (Tobj)type.Assembly.CreateInstance(type.FullName); PropertyInfo[] propertyInfos = type.GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { SetUserInfo(obj, propertyInfo, userInfo);//设置用户信息。 object[] objs = propertyInfo.GetCustomAttributes(typeof(FieldAttribute), true); if (objs.Length > 0) { FieldAttribute fieldAttribute = (FieldAttribute)objs[0]; if ((fieldAttribute.FieldAttributeType & fieldAttributeType) != fieldAttributeType) continue; //如果该属性没有指定的字段类型则跳过。 string columnName = fieldAttribute.FieldName == "" ? GetColumnName(row, propertyInfo.Name) : fieldAttribute.FieldName.ToUpper(); if (row.Cells.Exists(columnName) == false) { throw new Exception("实体类定义了字段名特性,但在Grid中没有找到该列名!"); } if (fieldAttribute.IsGetValue == true) { propertyInfo.SetValue(obj, row.Cells[columnName].Value.ToString(), null); } else { propertyInfo.SetValue(obj, row.Cells[columnName].Text.ToString(), null); } if (CheckIsNull(obj, propertyInfo) == true) { MessageUtil.ShowWarning(row.Cells[columnName].Column.Header.Caption + "不能为空!"); row.Activate(); row.Cells[columnName].Activate(); return default(Tobj); //引用类型返回null } } } return obj; } private string GetColumnName(UltraGridRow row, string propertyName) { foreach (UltraGridCell cell in row.Cells) { string columName = ""; string[] strAry = cell.Column.Key.Split('_'); for (int i = 0; i < strAry.Length; i++) { columName += strAry[i].Substring(0, 1).ToUpper() + strAry[i].Substring(1).ToLower(); } if (columName == propertyName) { return cell.Column.Key; } } return ""; } private bool CheckIsNull(Tobj obj, PropertyInfo propertyInfo) { object[] objs = propertyInfo.GetCustomAttributes(typeof(NullableAttribute), true); if (objs.Length > 0) { NullableAttribute nullableAttribute = (NullableAttribute)objs[0]; if (nullableAttribute.Nullable == false && (propertyInfo.GetValue(obj, null) == null || propertyInfo.GetValue(obj, null).ToString() == "")) { return true; } } return false; } private void SetUserInfo(Tobj obj, PropertyInfo propertyInfo, CoreUserInfo userInfo) { object[] objs = propertyInfo.GetCustomAttributes(typeof(UserInfoAttribute), true); if (objs.Length > 0) { UserInfoAttribute userInfoAttribute = (UserInfoAttribute)objs[0]; string strUserInfo = ""; switch (userInfoAttribute.UserInfoAttributeType) { case UserInfoAttributeType.Department: strUserInfo = userInfo.GetDepartment(); break; case UserInfoAttributeType.Deptid: strUserInfo = userInfo.GetDeptid(); break; case UserInfoAttributeType.GuidCode: strUserInfo = userInfo.GetGuidCode(); break; case UserInfoAttributeType.LoginID: strUserInfo = userInfo.GetLoginID(); break; case UserInfoAttributeType.LoginTime: strUserInfo = userInfo.GetLoginTime(); break; case UserInfoAttributeType.MachineIP: strUserInfo = userInfo.GetMachineIP(); break; case UserInfoAttributeType.MachineName: strUserInfo = userInfo.GetMachineName(); break; case UserInfoAttributeType.PurviewInfo: strUserInfo = userInfo.GetPurviewInfo(); break; case UserInfoAttributeType.Role: strUserInfo = userInfo.GetRole(); break; case UserInfoAttributeType.UserGroup: strUserInfo = userInfo.GetUserGroup(); break; case UserInfoAttributeType.UserGroupText: strUserInfo = userInfo.GetUserGroupText(); break; case UserInfoAttributeType.UserID: strUserInfo = userInfo.GetUserID(); break; case UserInfoAttributeType.UserName: strUserInfo = userInfo.GetUserName(); break; case UserInfoAttributeType.UserOrder: strUserInfo = userInfo.GetUserOrder(); break; case UserInfoAttributeType.UserOrderText: strUserInfo = userInfo.GetUserOrderText(); break; } propertyInfo.SetValue(obj, strUserInfo, null); } } /// /// Control中的控件集合转换为实体类,并验证是否为空。所能识别的特性:ControlAttribute, ControlAttributeType, NullableAttribute, CoreUserInfo。 /// /// /// /// public Tobj Convert(System.Windows.Forms.Control parentControl, CoreUserInfo userInfo, ControlAttributeType controlAttributeType) { Type type = typeof(Tobj); Tobj obj = (Tobj)type.Assembly.CreateInstance(type.FullName); PropertyInfo[] propertyInfos = type.GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { SetUserInfo(obj, propertyInfo, userInfo);//设置用户信息。 object[] objs = propertyInfo.GetCustomAttributes(typeof(ControlAttribute), true); if (objs.Length > 0) { ControlAttribute controlAttribute = (ControlAttribute)objs[0]; if ((controlAttribute.ControlAttributeType & controlAttributeType) != controlAttributeType) continue; //如果该属性没有指定的字段类型则跳过。 string controlName = controlAttribute.ControlName; System.Windows.Forms.Control[] childControls = parentControl.Controls.Find(controlName, true); System.Windows.Forms.Control childControl = null; if (childControls.Length == 0) { throw new Exception("实体类定义了字段名特性,但在parentControl中没有找到该控件名称!"); } else { childControl = childControls[0]; } string strValue = GetControlValue(childControl, controlAttribute.IsGetValue); propertyInfo.SetValue(obj, strValue, null); if (CheckIsNull(obj, propertyInfo) == true) { if (childControl is LabelControlInterface) { LabelControlInterface labelControl = (LabelControlInterface)childControl; if (labelControl is LabelComboBox) { MessageUtil.ShowWarning("请选择" + labelControl.Caption + "!"); } else { MessageUtil.ShowWarning(labelControl.Caption + "不能为空!"); } } else { throw new Exception(childControl.Name + ":不能为空!"); } childControl.Focus();//combobox下拉 return default(Tobj); //引用类型返回null } } } return obj; } private string GetControlValue(System.Windows.Forms.Control control, bool isGetValue) { switch (control.GetType().FullName) { case "System.Windows.Forms.TextBox": return ((TextBox)control).Text.Trim(); case "System.Windows.Forms.ComboBox": if (isGetValue) { if (((ComboBox)control).SelectedValue != null) { return ((ComboBox)control).SelectedValue.ToString(); } break; } else { return ((ComboBox)control).Text.Trim(); } case "Core.Mes.Client.Comm.Control.DataFilterControl": if (isGetValue) { if (((DataFilterControl)control).SelectedValue != null) { return ((DataFilterControl)control).SelectedValue.ToString(); } break; } else { return ((DataFilterControl)control).Text.Trim(); } case "Core.Mes.Client.Comm.Control.LabelComboBox": if (isGetValue) { if (((LabelComboBox)control).SelecteValue != null) { return ((LabelComboBox)control).SelecteValue.ToString(); } break; } else { return ((LabelComboBox)control).Text.Trim(); } case "Core.Mes.Client.Comm.Control.LabelTextBox": return ((LabelTextBox)control).Text.Trim(); case "Core.Mes.Client.Comm.Control.LabelCheckBox": return ((LabelCheckBox)control).Checked.ToString(); case "Core.Mes.Client.Comm.Control.LabelNumericText": return ((LabelNumericText)control).Value.ToString().Trim(); case "Core.Mes.Client.Comm.Control.LabelDateTimePicker": return ((LabelDateTimePicker)control).Value.ToString("yyyy-MM-dd HH:mm:ss"); } return ""; } } }