Exemple #1
0
        /// <summary>
        /// 取属性成员
        /// </summary>
        /// <typeparam name="A">属性</typeparam>
        /// <typeparam name="T">类</typeparam>
        /// <returns></returns>
        public static List <ReportAttr> GetQuickDataBaseAttr <T>()
        {
            string            key    = "GetAllPAttr_" + typeof(ReportAttr).FullName + "_" + typeof(T).FullName;
            List <ReportAttr> result = (List <ReportAttr>)MemCach.GetCach(key);

            if (result != null)
            {
                return(result);
            }

            string tabName;

            object[] os = typeof(T).GetCustomAttributes(typeof(ReportAttr), true);
            if (os.Length > 0)
            {
                tabName = ((ReportAttr)os[0]).TableName;
            }
            else
            {
                tabName = typeof(T).Name;
            }

            result = new List <ReportAttr>();

            PropertyInfo[] propertyinfos = typeof(T).GetProperties();

            propertyinfos.ToList().ForEach(p =>
            {
                object[] obs = p.GetCustomAttributes(typeof(ReportAttr), true);
                if (obs.Length > 0)
                {
                    ReportAttr attr = (ReportAttr)obs[0];
                    attr.TableName  = tabName;
                    if (string.IsNullOrWhiteSpace(attr.Column))
                    {
                        attr.Column = p.Name;
                    }
                    if (string.IsNullOrWhiteSpace(attr.ColumnName))
                    {
                        attr.ColumnName = p.Name;
                    }
                    PropertyInfo pp = attr.GetType().GetProperty("Property");
                    if (pp != null)
                    {
                        pp.SetValue(attr, p, null);
                    }

                    result.Add((ReportAttr)obs[0]);
                }
            });

            MemCach.AddCach(key, result);
            return(result);
        }
Exemple #2
0
        public CachDataContextMoudel()
        {
            rpAttrList = Comm.CommFun.GetQuickDataBaseAttr <T>();
            if (rpAttrList.Count == 0)
            {
                throw new Exception("无法生成缓存数据表对象!");
            }

            keyColumn = rpAttrList.Find(p => p.isKey);

            if (keyColumn == null)
            {
                throw new Exception("缺少表自增主键或者唯一主键,无法创建缓存数据表对象。");
            }

            readWriteLock.EnterWriteLock();
            try
            {
                //for (int i = 0; i < cachDataSet.Tables.Count; i++)
                //{
                //    if (cachDataSet.Tables[i].TableName.Equals(typeof(T).FullName))
                //    {
                //        cachTable = cachDataSet.Tables[i];
                //    }
                //}

                if (cachTable == null)
                {
                    cachTable = CreateCachTable();
                    //cachDataSet.Tables.Add(cachTable);
                }

                if (cachTable.Rows.Count > 0)
                {
                    maxKeyID = int.Parse(cachTable.Rows[cachTable.Rows.Count - 1][keyColumn.Column].ToString());
                }
                else
                {
                    maxKeyID = 0;
                }
            }
            finally
            {
                readWriteLock.ExitWriteLock();
            }
        }