Exemplo n.º 1
0
        public static T ExpandObject <T>(object o, Func <FieldInfo, IDataRecord, object> customApply) where T : class, new()
        {
            Type       t             = typeof(T);
            T          returnObject  = (T)o;
            string     dictionaryKey = string.Empty;
            SqlCommand sqlCommand;

            foreach (FieldInfo fieldInfo in returnObject.GetType().GetFields())
            {
                if (Attribute.IsDefined(fieldInfo, typeof(DictionaryMapAttribute)))
                {
                    DictionaryMapAttribute dictionaryMapAttribute = (DictionaryMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DictionaryMapAttribute));
                    using (DataManager.Current.OpenConnection())
                    {
                        if (!dictionaryMapAttribute.IsStoredProcedure)
                        {
                            sqlCommand = DataManager.CreateCommand(dictionaryMapAttribute.Command);
                        }
                        else
                        {
                            sqlCommand = DataManager.CreateCommand(dictionaryMapAttribute.Command, CommandType.StoredProcedure);
                        }
                        foreach (SqlParameter param in sqlCommand.Parameters)
                        {
                            string fieldName = param.ParameterName.Substring(1); //without the "@"
                            param.Value = returnObject.GetType().GetField(fieldName).GetValue(returnObject);
                        }

                        fieldInfo.SetValue(returnObject, GetDictionryObject(fieldInfo, sqlCommand.ExecuteReader()));
                    }
                }
            }
            return((T)returnObject);
        }
Exemplo n.º 2
0
        public static IDictionary GetDictionryObject(FieldInfo fieldInfo, IDataReader sqlDataReader)
        {
            string dictionaryKey = string.Empty;

            if (!fieldInfo.FieldType.IsGenericType || fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(IDictionary))
            {
                throw new Exception("This is not generic Dictionary");
            }

            Type keyElement  = fieldInfo.FieldType.GetGenericArguments()[0];
            Type typeElement = fieldInfo.FieldType.GetGenericArguments()[1];

            if (Attribute.IsDefined(fieldInfo, typeof(DictionaryMapAttribute)))
            {
                DictionaryMapAttribute dictionaryMapAttribute = (DictionaryMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DictionaryMapAttribute));
                dictionaryKey = dictionaryMapAttribute.DictionaryKey;
            }
            else
            {
                throw new Exception("DictionaryMapatrribute not defined");
            }


            IDictionary returnObject = (IDictionary)Activator.CreateInstance(fieldInfo.FieldType);

            if (typeElement.IsGenericType)
            {
                IList list          = (IList)Activator.CreateInstance(typeElement);
                int?  lastAccountId = null;
                while (sqlDataReader.Read())
                {
                    object currentItem = Activator.CreateInstance(typeElement.GetGenericArguments()[0]);
                    if (lastAccountId != null)
                    {
                        if (lastAccountId != (int)sqlDataReader[dictionaryKey])
                        {
                            returnObject.Add(lastAccountId, list);
                            list = (IList)Activator.CreateInstance(typeElement);
                        }
                    }
                    foreach (FieldInfo f in typeElement.GetGenericArguments()[0].GetFields())
                    {
                        if (Attribute.IsDefined(f, typeof(FieldMapAttribute)))
                        {
                            FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(f, typeof(FieldMapAttribute));
                            object            val = sqlDataReader[fieldMapAttribute.FieldName];
                            if (val is DBNull)
                            {
                                f.SetValue(currentItem, null);
                            }
                            else
                            {
                                f.SetValue(currentItem, val);
                            }
                        }
                    }
                    list.Add(currentItem);
                    lastAccountId = (int)sqlDataReader[dictionaryKey];
                }
                if (lastAccountId != null)
                {
                    returnObject.Add(lastAccountId, list);
                }
            }
            return(returnObject);
        }