using System; using System.Collections.Generic; using System.Linq; using System.Text; using CoreFS.CA06; using CoreFS.SA06; using System.Reflection; using System.Collections; using Infragistics.Win.UltraWinGrid; using System.ComponentModel; using Core.Mes.Client.Comm.Attribute; namespace Core.Mes.Client.Comm.Tool { /// /// 实体类帮助类 /// public class EntityHelper { /// /// 获取实体类集合 /// /// 实体类 /// 服务端方法ID /// 参数 /// 平台操作对象 /// public static DataSourceList GetData(string methodId, object[] param, OpeBase ob) { try { DataSourceList listResult = new DataSourceList(); CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = methodId.Substring(0, methodId.LastIndexOf(".")); ccp.MethodName = methodId.Substring(methodId.LastIndexOf(".") + 1); ccp.ServerParams = param; ccp = ob.ExecuteQuery(ccp, CoreInvokeType.Internal); string[] strColumns = (ccp.ReturnObject as CoreReturnSortObject).getColIdx(); string[] strColumnsEntity = ConvertColumnName(strColumns); ArrayList list = (ccp.ReturnObject as CoreReturnSortObject).getResult(); Type type = typeof(T); Dictionary propertyInfoDic = new Dictionary(); for (int j = 0; j < strColumns.Length; j++) { string column = strColumns[j]; string columnEntity = strColumnsEntity[j]; PropertyInfo propertyInfo = type.GetProperty(columnEntity); if (propertyInfo == null) continue; if (propertyInfoDic.ContainsKey(column)) { propertyInfoDic[column] = propertyInfo; } else { propertyInfoDic.Add(column, propertyInfo); } } for (int i = 0; i < list.Count; i++) { Hashtable hashtable = (Hashtable)list[i]; ArrayList list2 = new ArrayList(); T obj = (T)type.Assembly.CreateInstance(type.FullName); foreach (KeyValuePair propertyInfoKV in propertyInfoDic) { object obj2 = hashtable[propertyInfoKV.Key]; PropertyInfo propertyInfo = propertyInfoKV.Value; if ((obj2 != null) && typeof(Hashtable).Equals(obj2.GetType())) { Hashtable hashtable2 = (Hashtable)obj2; propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, hashtable2["value"]), null); } else { propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, obj2), null); } } listResult.Add(obj); } return listResult; } catch (Exception ex) { throw ex; } finally { } } private static object ConvertDataType(Type type, object objValue) { if (type == typeof(bool)) { bool result; if (bool.TryParse(objValue.ToString(), out result)) { return result; } else { return false; } } else if(type == typeof(bool?)) { bool result; if (bool.TryParse(objValue.ToString(), out result)) { return result; } else { return null; } } else if (type == typeof(int?)) { if (objValue == null) { return null; } else { return int.Parse(objValue.ToString()); } } else if (type == typeof(long?)) { if (objValue == null) { return null; } else { return long.Parse(objValue.ToString()); } } else if (type == typeof(short?)) { if (objValue == null) { return null; } else { return short.Parse(objValue.ToString()); } } else if (type == typeof(float?)) { if (objValue == null) { return null; } else { return float.Parse(objValue.ToString()); } } else if (type == typeof(double?)) { if (objValue == null) { return null; } else { return double.Parse(objValue.ToString()); } } else if (type == typeof(decimal?)) { if (objValue == null) { return null; } else { return decimal.Parse(objValue.ToString()); } } else if (type == typeof(DateTime?)) { if (objValue == null) { return null; } else { return DateTime.Parse(objValue.ToString()); } } else { return objValue == null ? "" : objValue.ToString(); } } private static string[] ConvertColumnName(string[] strColumns) { string[] strs = new string[strColumns.Length]; for (int i = 0; i < strColumns.Length; i++ ) { strs[i] = GetLanguageFormat(strColumns[i], false); } return strs; } private static string GetLanguageFormat(string strName, bool isLowerBegin) { string[] strs = strName.Split('_'); strName = ""; for (int i = 0; i < strs.Length; i++) { if (i == 0 && isLowerBegin) { strName += strs[i].ToLower(); } else { strName += strs[i].Substring(0, 1).ToUpper() + strs[i].Substring(1).ToLower(); } } return strName; } /// /// 复制实体类数据(源数据可以为任何类型,该方法只赋值属性名称相同的属性。) /// /// 实体类 /// 源对象 public static T CopyEntity(object srcObj) { try { if (srcObj == null) return default(T); T Desobj = (T)typeof(T).Assembly.CreateInstance(typeof(T).FullName); PropertyInfo[] srcPropertyInfos = srcObj.GetType().GetProperties(); foreach (PropertyInfo srcPropertyInfo in srcPropertyInfos) { PropertyInfo DespropertyInfo = Desobj.GetType().GetProperty(srcPropertyInfo.Name); if (DespropertyInfo == null) continue; if (DespropertyInfo.CanWrite) { DespropertyInfo.SetValue(Desobj, srcPropertyInfo.GetValue(srcObj, null), null); } } return Desobj; } catch (Exception ex) { throw; } } /// /// 复制实体类数据(源数据可以为任何类型,该方法只赋值属性名称相同的属性。) /// /// 实体类 /// 源对象 public static void CopyEntity(object srcObj, T descObj) { try { if (srcObj == null) { descObj = default(T); return; } PropertyInfo[] srcPropertyInfos = srcObj.GetType().GetProperties(); foreach (PropertyInfo srcPropertyInfo in srcPropertyInfos) { PropertyInfo DespropertyInfo = descObj.GetType().GetProperty(srcPropertyInfo.Name); if (DespropertyInfo == null) continue; if (DespropertyInfo.CanWrite) { DespropertyInfo.SetValue(descObj, srcPropertyInfo.GetValue(srcObj, null), null); } } } catch (Exception ex) { throw; } } /// /// 复制实体类集合 /// /// 实体类类型 /// 数据源 /// 复制结果 public static DataSourceList CopyEntitys(List srcObjs) { T[] objs = new T[srcObjs.Count]; srcObjs.CopyTo(objs); DataSourceList desObjs = new DataSourceList(); desObjs.AddRange(objs); return desObjs; } /// /// 比较实体类的内容是否完全一致 /// /// 实体类类型 /// 比较对象 /// 比较对象2 /// 结果 public static bool Compare(T obj, T obj2) { PropertyInfo[] propertyInfos = obj.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { if (propertyInfo.GetValue(obj, null).ToString2() != propertyInfo.GetValue(obj2, null).ToString2()) { return false; } } return true; } /// /// 根据实体类中的DescriptionAttribute特性描述,转换为Grid列标题。 /// /// 实体类 /// 需要转换的UltraGridBand public static void ShowGridCaption(UltraGridBand gridBand) { try { PropertyInfo[] propertyInfos = typeof(T).GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { if (gridBand.Columns.Exists(propertyInfo.Name)) { //设置单元格为空时的值。 if (propertyInfo.PropertyType == typeof(string)) { gridBand.Columns[propertyInfo.Name].Nullable = Infragistics.Win.UltraWinGrid.Nullable.EmptyString; } object[] desAttributes = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (desAttributes.Length > 0) { gridBand.Columns[propertyInfo.Name].Header.Caption = ((DescriptionAttribute)desAttributes[0]).Description; } object[] dataHiddenAttributes = propertyInfo.GetCustomAttributes(typeof(DataHiddenAttribute), false); if (dataHiddenAttributes.Length > 0) { DataHiddenAttribute nullableAttribute = (DataHiddenAttribute)dataHiddenAttributes[0]; gridBand.Columns[propertyInfo.Name].Hidden = nullableAttribute.DataHidden; } } } } catch (Exception ex) { throw; } } /// /// UltraGrid列是否隐藏 /// isShow 为true隐藏,false显示 /// UltraGrid /// 隐藏列 public static void setColumnShowOrHidden(UltraGrid ugr, string[] keys, bool isShow) { if (keys == null || keys.Length == 0) { foreach (UltraGridColumn ugc in ugr.DisplayLayout.Bands[0].Columns) { ugc.Hidden = false; } } else { keys.ToArray(); foreach (UltraGridColumn ugc in ugr.DisplayLayout.Bands[0].Columns) { if (keys.Contains(ugc.Key)) { ugc.Hidden = isShow; } } } } /// /// 建立实体类的关系数据 /// /// 主实体类 /// 从实体类 /// 主实体类对象集合 /// 从实体类对象集合 /// 关联关系属性 /// 关联关系属性 public static void AddEntityRelation(List parentObjs, List childObjs, string parentName, string childName) { try { PropertyInfo parentNameProperty = typeof(T).GetProperty(parentName); PropertyInfo childNameProperty = typeof(T2).GetProperty(childName); if (parentObjs.Count == 0 || childObjs.Count == 0) return; PropertyInfo childCollectionProperty = null; foreach (PropertyInfo tempParentProperty in typeof(T).GetProperties()) { if (tempParentProperty.PropertyType.Equals(childObjs.GetType()) || tempParentProperty.PropertyType.Equals(childObjs.GetType().BaseType)) { childCollectionProperty = tempParentProperty; } } if (childCollectionProperty == null) return; foreach (T obj in parentObjs) { List childCollection = new List(); for (int i = 0; i < childObjs.Count; i++) { T2 obj2 = childObjs[i]; if (parentNameProperty.GetValue(obj, null).ToString() == childNameProperty.GetValue(obj2, null).ToString()) { childCollection.Add(obj2); childObjs.Remove(obj2); i--; } } childCollectionProperty.SetValue(obj, childCollection, null); } } catch (Exception ex) { throw; } } /// /// 建立实体类的关系数据 /// /// 主实体类 /// 从实体类 /// 主实体类对象集合 /// 从实体类对象集合 /// 关联关系属性 /// 关联关系属性 public static void AddEntityRelation(List parentObjs, List childObjs, string[] parentNames, string[] childNames) { try { if (parentObjs.Count == 0 || childObjs.Count == 0) return; if (parentNames.Length == 0 || childNames.Length == 0) { throw new Exception("关联属性不能为空!"); } if (parentNames.Length != childNames.Length) { throw new Exception("关联属性个数不一致!"); } PropertyInfo childCollectionProperty = null; foreach (PropertyInfo tempParentProperty in typeof(T).GetProperties()) { if (tempParentProperty.PropertyType.Equals(childObjs.GetType()) || tempParentProperty.PropertyType.Equals(childObjs.GetType().BaseType)) { childCollectionProperty = tempParentProperty; } } if (childCollectionProperty == null) return; foreach (T obj in parentObjs) { List childCollection = new List(); for (int i = 0; i < childObjs.Count; i++) { T2 obj2 = childObjs[i]; bool isSame = true; for (int j = 0; j < parentNames.Length; j++) { PropertyInfo parentNameProperty = typeof(T).GetProperty(parentNames[j]); PropertyInfo childNameProperty = typeof(T2).GetProperty(childNames[j]); if (parentNameProperty.GetValue(obj, null).ToString() != childNameProperty.GetValue(obj2, null).ToString()) { isSame = false; break; } } if (isSame) { childCollection.Add(obj2); childObjs.Remove(obj2); i--; } } childCollectionProperty.SetValue(obj, childCollection, null); } } catch (Exception ex) { throw; } } /// /// 验证实体类的内容 /// /// 实体类 /// 输入提示消息 /// 错误字段 /// 验证结果 public static bool CheckEntity(object obj, out string msg, out string fieldName) { msg = ""; fieldName = ""; PropertyInfo[] propertyInfos = obj.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { object[] nullableAttributes = propertyInfo.GetCustomAttributes(typeof(NullableAttribute), false); object[] dataLengthAttributes = propertyInfo.GetCustomAttributes(typeof(DataLengthAttribute), false); object[] descAtrributes = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); object proValue = propertyInfo.GetValue(obj, null); DescriptionAttribute descriptionAttribute = (DescriptionAttribute)descAtrributes[0]; if (nullableAttributes.Length > 0 && descAtrributes.Length > 0) { NullableAttribute nullableAttribute = (NullableAttribute)nullableAttributes[0]; if (nullableAttribute.Nullable == false && (proValue == null || proValue.ToString() == "")) { msg = descriptionAttribute.Description + "不能为空!"; fieldName = propertyInfo.Name; return false; } } //只验证字符串的长度。 if (propertyInfo.PropertyType == typeof(string)) { if (dataLengthAttributes.Length > 0 && descAtrributes.Length > 0) { DataLengthAttribute dataLengthAttribute = (DataLengthAttribute)dataLengthAttributes[0]; int strSize = GetCharacterSize(proValue == null ? "" : proValue.ToString()); if (strSize > dataLengthAttribute.DataLength) { msg = descriptionAttribute.Description + "超出系统定义的字符长度!"; fieldName = propertyInfo.Name; return false; } //if() } } } return true; } private static int GetCharacterSize(string str) { return Encoding.Unicode.GetByteCount(str); } /// /// 根据属性名称获取对象中的值。 /// /// 对象 /// 属性名称 /// 返回值 public static object GetEntityPropertyValue(object obj, string propertyName) { PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName); if (propertyInfo == null) return null; else return propertyInfo.GetValue(obj, null); } } }