Exemplo n.º 1
0
 /// <summary>
 /// Gets the column names.the key is ColumnName and the value is PropertyName.
 /// </summary>
 public static ConcurrentDictionary <string, string> GetColumn2PropNameDics(Data.DirverType dirverType, Type modelType)
 {
     return(MemcacheHelper <ConcurrentDictionary <string, string> > .ReadAndWrite("[GetColumn2PropNameDics][" + modelType.FullName + "][" + dirverType + "]", () =>
     {
         ConcurrentDictionary <string, string> dic = new ConcurrentDictionary <string, string>();
         var props = modelType.GetProperties().ToList();
         foreach (var prop in props)
         {
             if (!prop.CanRead || !prop.CanWrite)
             {
                 continue;
             }
             var attrs = prop.GetCustomAttributes(typeof(Common.Attribute.ColumnAttribute), true);
             if (attrs == null)
             {
                 dic.TryAdd(prop.Name, prop.Name); continue;
             }
             var lstAttrs = attrs.ToList().Select(a => (Common.Attribute.ColumnAttribute)a);
             var targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == dirverType);
             if (targetAttr == null)
             {
                 targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == Data.DirverType.UnKnown);
             }
             if (targetAttr != null && !string.IsNullOrWhiteSpace(targetAttr.ColumnName))
             {
                 dic[targetAttr.ColumnName] = prop.Name;
             }
             else
             {
                 dic[prop.Name] = prop.Name;
             }
         }
         return dic;
     }));
 }
Exemplo n.º 2
0
        /// <summary>
        /// convert to object list.
        /// </summary>
        public static List <T> GetList <T>(DbDataReader dbReader, Data.DirverType attrDirverType = Data.DirverType.UnKnown)
        {
            List <T> lst          = new List <T>();
            var      type         = typeof(T);
            var      typeInfo     = ReflectionCacheManager.Instance.GetAndSet(type);
            var      noArgument   = new object[] { };
            var      col2PropDics = AttributeHelper.GetColumn2PropNameDics(attrDirverType, type);
            List <IDynamicPropertyInfo> propQueue = new List <IDynamicPropertyInfo>();
            List <int> indexs = new List <int>();

            while (dbReader.Read())
            {
                lock (lockSync)
                {
                    T instance = (T)typeInfo.DynamicConstructorInfo.Invoke(noArgument);
                    if (propQueue.Count != 0)
                    {
                        int getIndex = 0;
                        foreach (var columnIndex in indexs)
                        {
                            var    prop    = propQueue[getIndex++];
                            object dbValue = dbReader.GetValue(columnIndex);
                            object value   = TypeConverter.To(prop.ObjectType, dbValue);
                            prop.SetValue(instance, value, null);
                        }
                    }
                    else
                    {
                        for (int columnIndex = 0; columnIndex < dbReader.FieldCount; columnIndex++)
                        {
                            string dbColumnName = dbReader.GetName(columnIndex);
                            if (!col2PropDics.ContainsKey(dbColumnName))
                            {
                                continue;
                            }
                            string propertyName = col2PropDics[dbColumnName];
                            if (!typeInfo.DynamicPropertyDics.ContainsKey(propertyName))
                            {
                                continue;
                            }
                            indexs.Add(columnIndex);
                            IDynamicPropertyInfo prop = typeInfo.DynamicPropertyDics[propertyName];
                            propQueue.Add(prop);
                            object dbValue = dbReader.GetValue(columnIndex);
                            object value   = TypeConverter.To(prop.ObjectType, dbValue);
                            prop.SetValue(instance, value, null);
                        }
                    }
                    lst.Add(instance);
                }
            }
#if NETFRAMEWORK
            dbReader.Close();
#endif
            dbReader.Dispose();
            return(lst);
        }
Exemplo n.º 3
0
        /// <summary>
        /// convert to object list
        /// </summary>
        public static List <T> GetList <T>(MDataTable table, Data.DirverType attrDirverType = Data.DirverType.UnKnown) where T : new()
        {
            List <T> result;

            if (table == null)
            {
                result = new List <T>();
            }
            else
            {
                List <T>       list           = new List <T>();
                Type           typeFromHandle = typeof(T);
                var            attrDic        = AttributeHelper.GetProp2ColumnNameDics(attrDirverType, typeFromHandle);
                PropertyInfo[] properties     = typeFromHandle.GetProperties();
                foreach (var dataRow in table.Rows)
                {
                    T t = (T)((object)Activator.CreateInstance(typeFromHandle));
                    PropertyInfo[] array = properties;
                    for (int i = 0; i < array.Length; i++)
                    {
                        PropertyInfo propertyInfo = array[i];
                        var          columnName   = propertyInfo.Name;
                        if (attrDic.ContainsKey(columnName))
                        {
                            columnName = attrDic[columnName];
                        }
                        if (table.ContainsColumn(columnName))
                        {
                            if (propertyInfo.CanWrite)
                            {
                                var cellValue = dataRow[columnName].Value;
                                if (cellValue is DBNull || cellValue == null)
                                {
                                    propertyInfo.SetValue(t, null, null);
                                }
                                else
                                {
                                    var obj = TypeConverter.To(propertyInfo.PropertyType, cellValue);
                                    propertyInfo.SetValue(t, obj, null);
                                }
                            }
                        }
                    }
                    list.Add(t);
                }
                result = list;
            }
            return(result ?? new List <T>());
        }
Exemplo n.º 4
0
 /// <summary>
 /// Gets the primary keys.
 /// </summary>
 public static List <string> GetPrimaryKeys(Data.DirverType dirverType, Type modelType, bool isColumnNameOrPropertyName = true)
 {
     return(MemcacheHelper <List <string> > .ReadAndWrite("[GetPrimaryKeys][" + modelType.FullName + "][" + dirverType + "][" + isColumnNameOrPropertyName + "]", () =>
     {
         List <string> lst = new List <string>();
         var props = modelType.GetProperties().ToList();
         foreach (var prop in props)
         {
             if (!prop.CanRead || !prop.CanWrite)
             {
                 continue;
             }
             var attrs = prop.GetCustomAttributes(typeof(Common.Attribute.ColumnAttribute), true);
             if (attrs == null)
             {
                 continue;
             }
             var lstAttrs = attrs.ToList().ConvertToAll(a => (Common.Attribute.ColumnAttribute)a);
             var targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == dirverType);
             if (targetAttr == null)
             {
                 targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == Data.DirverType.UnKnown);
             }
             if (targetAttr != null && targetAttr.PrimaryKey)
             {
                 if (isColumnNameOrPropertyName && !string.IsNullOrWhiteSpace(targetAttr.ColumnName))
                 {
                     lst.Add(targetAttr.ColumnName);
                 }
                 else
                 {
                     lst.Add(prop.Name);
                 }
             }
         }
         return lst;
     }));
 }