/// <summary> /// 获取对象映射信息 /// </summary> /// <param name="objType">对象类型</param> /// <returns></returns> public static ObjectMappingInfo GetObjectMapping(Type objType) { String cacheKey = mObjectMapCacheKey + objType.FullName; try { ObjectMappingInfo objMap = hashtable[cacheKey] as ObjectMappingInfo; if (objMap == null) { objMap = new ObjectMappingInfo(); objMap.ObjectType = objType.FullName; objMap.PrimaryKey = GetPrimaryKey(objType); objMap.TableName = GetTableName(objType); foreach (PropertyInfo objProperty in objType.GetProperties()) { objMap.Properties.Add( objProperty.Name.ToUpperInvariant(), objProperty); objMap.ColumnNames.Add( objProperty.Name.ToUpperInvariant(), GetColumnName(objProperty)); } hashtable.Add(cacheKey, objMap); } return(objMap); } catch { return(null); } }
/// <summary> /// 创建对象实例 /// </summary> /// <param name="objType">要创建的对象类型</param> /// <param name="isInitialization">是否初始化对象实例</param> /// <returns>返回创建的对象实例</returns> public static Object CreateObject(Type objType, Boolean isInitialization = false) { try { Object objObject = Activator.CreateInstance(objType); if (isInitialization) { ObjectMappingInfo.InitializationObject(objObject); } return(objObject); } catch { throw; } }
/// <summary> /// 初始化对象各属性 /// </summary> /// <param name="objObject">需初始化的对象</param> /// <param name="objType">需初始化的对象的类型</param> /// <returns>初始化后的对象</returns> public static Object InitializationObject(Object objObject, Type objType) { ObjectMappingInfo objMapping = GetObjectMapping(objType); if (objMapping == null) { return(null); } try { foreach (PropertyInfo objPropertyInfo in objMapping.Properties.Values) { if (objPropertyInfo.CanWrite) { objPropertyInfo.SetValue(objObject, Null.GetNull(objPropertyInfo), null); } } return(objObject); } catch { return(null); } }
/// <summary> /// 将IDataReader中数据填充到对象中 /// </summary> /// <param name="objObject">要填充的对象</param> /// <param name="dataReader">IDataReader</param> private static void FillObjectFromReader(Object objObject, IDataReader dataReader) { PropertyInfo objPropertyInfo = null; Type objPropertyType = null; Object objDataValue; Type objDataType; Int32 intIndex; ObjectMappingInfo objMappingInfo = ObjectMappingInfo.GetObjectMapping(objObject.GetType()); if (objMappingInfo == null) { objObject = null; return; } try { for (intIndex = 0; intIndex <= dataReader.FieldCount - 1; intIndex++) { if (objMappingInfo.Properties.TryGetValue(dataReader.GetName(intIndex).ToUpperInvariant(), out objPropertyInfo)) { objPropertyType = objPropertyInfo.PropertyType; if (objPropertyInfo.CanWrite) { objDataValue = dataReader.GetValue(intIndex); objDataType = objDataValue.GetType(); if (objDataValue == null || objDataValue == DBNull.Value) { objPropertyInfo.SetValue(objObject, Null.GetNull(objPropertyInfo), null); } else if (objPropertyType.Equals(objDataType)) { objPropertyInfo.SetValue(objObject, objDataValue, null); } else { if (objPropertyType == typeof(Char)) { if (objDataValue.ToString().Length > 1) { objPropertyInfo.SetValue(objObject, objDataValue.ToString().ToCharArray()[0], null); } else { objPropertyInfo.SetValue(objObject, Null.NullChar, null); } } else if (objPropertyType.BaseType.Equals(typeof(Enum))) { if (Regex.IsMatch(objDataValue.ToString(), "^\\d+$")) { objPropertyInfo.SetValue(objObject, Enum.ToObject(objPropertyType, Convert.ToInt32(objDataValue)), null); } else { objPropertyInfo.SetValue(objObject, Enum.ToObject(objPropertyType, objDataValue), null); } } else if (objPropertyType == typeof(Guid)) { objPropertyInfo.SetValue(objObject, Convert.ChangeType(new Guid(objDataValue.ToString()), objPropertyType), null); } else if (objPropertyType == typeof(Version)) { objPropertyInfo.SetValue(objObject, new Version(objDataValue.ToString()), null); } else if (objPropertyType == objDataType) { objPropertyInfo.SetValue(objObject, objDataValue, null); } else { //等=1 为true 非1为false if (objPropertyType == typeof(Boolean)) { if (objDataValue.ToString() == "1") { objDataValue = true; } else { objDataValue = false; } } if (((objDataValue is Int32) || (objDataValue is Single) || (objDataValue is Decimal)) && objPropertyType == typeof(String)) { objDataValue = objDataValue.ToString(); } objPropertyInfo.SetValue(objObject, Convert.ChangeType(objDataValue, objPropertyType), null); } } } } } } catch { throw; } }