| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- 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
- {
- /// <summary>
- /// 实体类帮助类
- /// </summary>
- public class EntityHelper
- {
- /// <summary>
- /// 获取实体类集合
- /// </summary>
- /// <typeparam name="T">实体类</typeparam>
- /// <param name="methodId">服务端方法ID</param>
- /// <param name="param">参数</param>
- /// <param name="ob">平台操作对象</param>
- /// <returns></returns>
- public static DataSourceList<T> GetData<T>(string methodId, object[] param, OpeBase ob)
- {
- try
- {
- DataSourceList<T> listResult = new DataSourceList<T>();
- 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<string, PropertyInfo> propertyInfoDic = new Dictionary<string, PropertyInfo>();
- 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<string, PropertyInfo> 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;
- }
- /// <summary>
- /// 复制实体类数据(源数据可以为任何类型,该方法只赋值属性名称相同的属性。)
- /// </summary>
- /// <typeparam name="T">实体类</typeparam>
- /// <param name="srcObj">源对象</param>
- public static T CopyEntity<T>(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;
- }
- }
- /// <summary>
- /// 复制实体类数据(源数据可以为任何类型,该方法只赋值属性名称相同的属性。)
- /// </summary>
- /// <typeparam name="T">实体类</typeparam>
- /// <param name="srcObj">源对象</param>
- public static void CopyEntity<T>(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;
- }
- }
- /// <summary>
- /// 复制实体类集合
- /// </summary>
- /// <typeparam name="T">实体类类型</typeparam>
- /// <param name="srcObjs">数据源</param>
- /// <returns>复制结果</returns>
- public static DataSourceList<T> CopyEntitys<T>(List<T> srcObjs)
- {
- T[] objs = new T[srcObjs.Count];
- srcObjs.CopyTo(objs);
- DataSourceList<T> desObjs = new DataSourceList<T>();
- desObjs.AddRange(objs);
- return desObjs;
- }
- /// <summary>
- /// 比较实体类的内容是否完全一致
- /// </summary>
- /// <typeparam name="T">实体类类型</typeparam>
- /// <param name="obj">比较对象</param>
- /// <param name="obj2">比较对象2</param>
- /// <returns>结果</returns>
- public static bool Compare<T>(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;
- }
- /// <summary>
- /// 根据实体类中的DescriptionAttribute特性描述,转换为Grid列标题。
- /// </summary>
- /// <typeparam name="T">实体类</typeparam>
- /// <param name="gridBand">需要转换的UltraGridBand</param>
- public static void ShowGridCaption<T>(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;
- }
- }
- /// <summary>
- /// UltraGrid列是否隐藏
- /// </summary>isShow 为true隐藏,false显示
- /// <param name="ugr">UltraGrid</param>
- /// <param name="keys">隐藏列</param>
- 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;
- }
- }
- }
- }
- /// <summary>
- /// 建立实体类的关系数据
- /// </summary>
- /// <typeparam name="T">主实体类</typeparam>
- /// <typeparam name="T2">从实体类</typeparam>
- /// <param name="parentObjs">主实体类对象集合</param>
- /// <param name="childObjs">从实体类对象集合</param>
- /// <param name="parentName">关联关系属性</param>
- /// <param name="childName">关联关系属性</param>
- public static void AddEntityRelation<T, T2>(List<T> parentObjs, List<T2> 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<T2> childCollection = new List<T2>();
- 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;
- }
- }
- /// <summary>
- /// 建立实体类的关系数据
- /// </summary>
- /// <typeparam name="T">主实体类</typeparam>
- /// <typeparam name="T2">从实体类</typeparam>
- /// <param name="parentObjs">主实体类对象集合</param>
- /// <param name="childObjs">从实体类对象集合</param>
- /// <param name="parentName">关联关系属性</param>
- /// <param name="childName">关联关系属性</param>
- public static void AddEntityRelation<T, T2>(List<T> parentObjs, List<T2> 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<T2> childCollection = new List<T2>();
- 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;
- }
- }
- /// <summary>
- /// 验证实体类的内容
- /// </summary>
- /// <param name="obj">实体类</param>
- /// <param name="msg">输入提示消息</param>
- /// <param name="fieldName">错误字段</param>
- /// <returns>验证结果</returns>
- 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);
- }
- /// <summary>
- /// 根据属性名称获取对象中的值。
- /// </summary>
- /// <param name="obj">对象</param>
- /// <param name="propertyName">属性名称</param>
- /// <returns>返回值</returns>
- 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);
- }
- }
- }
|