コード例 #1
0
ファイル: CBO.cs プロジェクト: ithanshui/Common-2
 /// <summary>
 /// Sets the cache.
 /// </summary>
 /// <param name="cacheKey">The cache key.</param>
 /// <param name="objMap">The obj map.</param>
 /// <remarks></remarks>
 internal static void SetCache(string cacheKey, ObjectMappingInfo objMap)
 {
     if (Cache.ContainsKey(cacheKey))
     {
         Cache[cacheKey] = objMap;
     }
     else
     {
         Cache.Add(cacheKey, objMap);
     }
 }
コード例 #2
0
ファイル: CBO.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// 对象复制
        /// </summary>
        /// <param name="objObject">The obj object.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static object CloneObject(object objObject)
        {
            Type              objType        = objObject.GetType();
            object            objNewObject   = Activator.CreateInstance(objType);
            ObjectMappingInfo objMappingInfo = GetObjectMapping(objType);

            foreach (var kvp in objMappingInfo.Properties)
            {
                PropertyInfo objProperty = kvp.Value;
                if (objProperty.CanWrite)
                {
                    var objPropertyClone = objProperty.GetValue(objObject, null) as ICloneable;
                    if (objPropertyClone == null)
                    {
                        objProperty.SetValue(objNewObject, objProperty.GetValue(objObject, null), null);
                    }
                    else
                    {
                        objProperty.SetValue(objNewObject, objPropertyClone.Clone(), null);
                    }
                    var enumerable = objProperty.GetValue(objObject, null) as IEnumerable;
                    if (enumerable != null)
                    {
                        var list = objProperty.GetValue(objNewObject, null) as IList;
                        if (list != null)
                        {
                            foreach (object obj in enumerable)
                            {
                                list.Add(CloneObject(obj));
                            }
                        }
                        var dic = objProperty.GetValue(objNewObject, null) as IDictionary;
                        if (dic != null)
                        {
                            foreach (DictionaryEntry de in enumerable)
                            {
                                dic.Add(de.Key, CloneObject(de.Value));
                            }
                        }
                    }
                }
            }
            return(objNewObject);
        }
コード例 #3
0
ファイル: CBO.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Gets the object mapping.
        /// </summary>
        /// <param name="objType">Type of the obj.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static ObjectMappingInfo GetObjectMapping(Type objType)
        {
            string            cacheKey = ObjectMapCacheKey + objType.FullName;
            ObjectMappingInfo objMap   = DataCache.GetCache(cacheKey);

            if (objMap == null)
            {
                objMap = new ObjectMappingInfo
                {
                    ObjectType = objType.FullName,
                    PrimaryKey = GetPrimaryKey(objType),
                    TableName  = GetTableName(objType, "")
                };
                foreach (PropertyInfo objProperty in objType.GetProperties())
                {
                    objMap.Properties.Add(objProperty.Name.ToUpperInvariant(), objProperty);
                    objMap.ColumnNames.Add(objProperty.Name.ToUpperInvariant(), GetColumnName(objProperty));
                }
                DataCache.SetCache(cacheKey, objMap);
            }
            return(objMap);
        }
コード例 #4
0
ファイル: CBO.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Hydrates the object.
        /// </summary>
        /// <param name="hydratedObject">The hydrated object.</param>
        /// <param name="dr">The dr.</param>
        /// <remarks></remarks>
        private static void HydrateObject(object hydratedObject, IDataReader dr)
        {
            PropertyInfo      objPropertyInfo = null;
            Type              propType        = null;
            object            coloumnValue;
            Type              objDataType;
            int               intIndex;
            ObjectMappingInfo objMappingInfo = GetObjectMapping(hydratedObject.GetType());

            for (intIndex = 0; intIndex <= dr.FieldCount - 1; intIndex++)
            {
                if (objMappingInfo.Properties.TryGetValue(dr.GetName(intIndex).ToUpperInvariant(), out objPropertyInfo))
                {
                    propType = objPropertyInfo.PropertyType;
                    if (objPropertyInfo.CanWrite)
                    {
                        coloumnValue = dr.GetValue(intIndex);
                        objDataType  = coloumnValue.GetType();
                        if (coloumnValue == DBNull.Value)
                        {
                            objPropertyInfo.SetValue(hydratedObject, Null.SetNull(objPropertyInfo), null);
                        }
                        else if (propType.Equals(objDataType))
                        {
                            objPropertyInfo.SetValue(hydratedObject, coloumnValue, null);
                        }
                        else
                        {
                            if (propType.BaseType.Equals(typeof(Enum)))
                            {
                                if (Regex.IsMatch(coloumnValue.ToString(), "^\\d+$"))
                                {
                                    objPropertyInfo.SetValue(hydratedObject,
                                                             Enum.ToObject(propType, Convert.ToInt32(coloumnValue)),
                                                             null);
                                }
                                else
                                {
                                    objPropertyInfo.SetValue(hydratedObject, Enum.ToObject(propType, coloumnValue), null);
                                }
                            }
                            else if (propType == typeof(Guid))
                            {
                                objPropertyInfo.SetValue(hydratedObject,
                                                         Convert.ChangeType(new Guid(coloumnValue.ToString()), propType),
                                                         null);
                            }
                            else if (propType == typeof(Version))
                            {
                                objPropertyInfo.SetValue(hydratedObject, new Version(coloumnValue.ToString()), null);
                            }
                            else if (coloumnValue is IConvertible && propType.Name != "Nullable`1")
                            {
                                objPropertyInfo.SetValue(hydratedObject, Convert.ChangeType(coloumnValue, propType),
                                                         null);
                            }
                            else if (coloumnValue is IConvertible && propType.Name == "Nullable`1")
                            {
                                NullableConverter converter = new NullableConverter(propType);
                                objPropertyInfo.SetValue(hydratedObject, converter.ConvertFrom(coloumnValue.ToString()),
                                                         null);
                            }
                            else
                            {
                                // try explicit conversion
                                objPropertyInfo.SetValue(hydratedObject, coloumnValue, null);
                            }
                        }
                    }
                }
            }
        }