Exemplo n.º 1
0
        /// <summary>
        /// foreach数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="name"></param>
        /// <param name="db"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static List <T> MapForEach <T>(List <T> data, string name, DataContext db, ConfigModel config, int i = 1) where T : class, new()
        {
            var      result  = new List <T>();
            var      param   = new List <DbParameter>();
            var      dicName = DbCache.Get(config.CacheType, string.Format("{0}.foreach.name.{1}", name.ToLower(), i));
            var      type    = DbCache.Get(config.CacheType, string.Format("{0}.foreach.type.{1}", name.ToLower(), i));
            var      field   = DbCache.Get(config.CacheType, string.Format("{0}.foreach.field.{1}", name.ToLower(), i));
            var      sql     = DbCache.Get(config.CacheType, string.Format("{0}.foreach.sql.{1}", name.ToLower(), i));
            Assembly assembly;

            if (type.IndexOf(',') > 0)
            {
                var key = string.Format("ForEach.{0}", type.Split(',')[1]);
                if (DbCache.Exists(CacheType.Web, key))
                {
                    assembly = DbCache.Get <object>(CacheType.Web, key) as Assembly;
                }
                else
                {
                    assembly = AppDomain.CurrentDomain.GetAssemblies().ToList().Find(a => a.FullName.Split(',')[0] == type.Split(',')[1]);
                }
                if (assembly == null)
                {
                    assembly = Assembly.Load(type.Split(',')[1]);
                }
                if (assembly == null)
                {
                    return(data);
                }
                else
                {
                    DbCache.Set <object>(CacheType.Web, key, assembly);
                    if (assembly.GetType(type.Split(',')[0]) == null)
                    {
                        return(data);
                    }

                    foreach (var item in data)
                    {
                        var model      = Activator.CreateInstance(assembly.GetType(type.Split(',')[0]));
                        var list       = Activator.CreateInstance(typeof(List <>).MakeGenericType(assembly.GetType(type.Split(',')[0])));
                        var infoResult = BaseDic.PropertyInfo <T>().Find(a => a.PropertyType.FullName == list.GetType().FullName);
                        var dynSet     = new DynamicSet(model);

                        //param
                        param.Clear();
                        if (field.IndexOf(',') > 0)
                        {
                            foreach (var split in field.Split(','))
                            {
                                var infoField = BaseDic.PropertyInfo <T>().Find(a => a.Name.ToLower() == split);
                                var tempParam = DbProviderFactories.GetFactory(config).CreateParameter();
                                tempParam.ParameterName = split;
                                tempParam.Value         = infoField.GetValue(item, null);
                                param.Add(tempParam);
                            }
                        }
                        else
                        {
                            var infoField = BaseDic.PropertyInfo <T>().Find(a => a.Name.ToLower() == field);
                            var tempParam = DbProviderFactories.GetFactory(config).CreateParameter();
                            tempParam.ParameterName = field;
                            tempParam.Value         = infoField.GetValue(item, null);
                            param.Add(tempParam);
                        }

                        var tempData = db.ExecuteSql(sql, param.ToArray(), false);

                        foreach (var temp in tempData.DicList)
                        {
                            foreach (var info in model.GetType().GetProperties())
                            {
                                if (temp.GetValue(info.Name).ToStr() == "" && info.PropertyType.Name == "Nullable`1")
                                {
                                    continue;
                                }

                                if (info.PropertyType.Name == "Nullable`1" && info.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                                {
                                    dynSet.SetValue(model, info.Name, Convert.ChangeType(temp.GetValue(info.Name), Nullable.GetUnderlyingType(info.PropertyType)), config.IsPropertyCache);
                                }
                                else
                                {
                                    dynSet.SetValue(model, info.Name, Convert.ChangeType(temp.GetValue(info.Name), info.PropertyType), config.IsPropertyCache);
                                }
                            }

                            var method = list.GetType().GetMethod("Add", BindingFlags.Instance | BindingFlags.Public);
                            method.Invoke(list, new object[] { model });
                        }

                        infoResult.SetValue(item, list);
                        result.Add(item);
                    }
                    return(result);
                }
            }
            else
            {
                return(data);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// set value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <param name="dynSet"></param>
        /// <param name="dr"></param>
        /// <param name="info"></param>
        /// <param name="config"></param>
        private static T SetValue <T>(T item, DynamicSet <T> dynSet, DbDataReader dr, PropertyModel info, ConfigModel config)
        {
            try
            {
                var id = dr.GetOrdinal(config.DbType == DataDbType.Oracle ? info.Name.ToUpper() : info.Name);
                if (DataDbType.Oracle == config.DbType)
                {
                    object value    = null;
                    var    typeName = dr.GetDataTypeName(id).ToLower();
                    if (typeName == "clob" || typeName == "nclob")
                    {
                        dr.GetType().GetMethods().ToList().ForEach(m =>
                        {
                            if (m.Name == "GetOracleClob")
                            {
                                var param = new object[1];
                                param[0]  = id;
                                var temp  = m.Invoke(dr, param);
                                temp.GetType().GetMethods().ToList().ForEach(v =>
                                {
                                    if (v.Name == "get_Value")
                                    {
                                        value = v.Invoke(temp, null);
                                    }
                                });
                                temp.GetType().GetMethods().ToList().ForEach(v =>
                                {
                                    if (v.Name == "Close")
                                    {
                                        v.Invoke(temp, null);
                                    }
                                });
                                temp.GetType().GetMethods().ToList().ForEach(v =>
                                {
                                    if (v.Name == "Dispose")
                                    {
                                        v.Invoke(temp, null);
                                    }
                                });
                            }
                        });
                    }
                    else if (typeName == "blob")
                    {
                        dr.GetType().GetMethods().ToList().ForEach(m =>
                        {
                            if (m.Name == "GetOracleBlob")
                            {
                                var param = new object[1];
                                param[0]  = id;
                                var temp  = m.Invoke(dr, param);
                                temp.GetType().GetMethods().ToList().ForEach(v =>
                                {
                                    if (v.Name == "get_Value")
                                    {
                                        value = v.Invoke(temp, null);
                                    }
                                });
                                temp.GetType().GetMethods().ToList().ForEach(v =>
                                {
                                    if (v.Name == "Close")
                                    {
                                        v.Invoke(temp, null);
                                    }
                                });
                                temp.GetType().GetMethods().ToList().ForEach(v =>
                                {
                                    if (v.Name == "Dispose")
                                    {
                                        v.Invoke(temp, null);
                                    }
                                });
                            }
                        });
                    }
                    else
                    {
                        value = dr.GetValue(id);
                    }

                    if (!dr.IsDBNull(id))
                    {
                        if (info.PropertyType.Name == "Nullable`1" && info.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            dynSet.SetValue(item, info.Name, Convert.ChangeType(value, Nullable.GetUnderlyingType(info.PropertyType)), config.IsPropertyCache);
                        }
                        else
                        {
                            dynSet.SetValue(item, info.Name, Convert.ChangeType(value, info.PropertyType), config.IsPropertyCache);
                        }
                    }
                }
                else
                {
                    if (!dr.IsDBNull(id))
                    {
                        if (info.PropertyType.Name == "Nullable`1" && info.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            dynSet.SetValue(item, info.Name, Convert.ChangeType(dr.GetValue(id), Nullable.GetUnderlyingType(info.PropertyType)), config.IsPropertyCache);
                        }
                        else
                        {
                            dynSet.SetValue(item, info.Name, Convert.ChangeType(dr.GetValue(id), info.PropertyType), config.IsPropertyCache);
                        }
                    }
                }

                return(item);
            }
            catch { return(item); }
        }