Esempio n. 1
0
        public static List <IFilter> CreateFilters <TTemplate>(ExcelHeaderRow headerRow)
        {
            Type templateType = typeof(TTemplate);
            var  key          = templateType;

            if (Table[key] != null)
            {
                return((List <IFilter>)Table[key]);
            }

            List <IFilter>             filters        = new List <IFilter>();
            List <BaseFilterAttribute> attrs          = new List <BaseFilterAttribute>();
            TypeFilterInfo             typeFilterInfo = TypeFilterInfoFlyweight.CreateInstance(typeof(TTemplate), headerRow);

            typeFilterInfo.PropertyFilterInfos.ForEach(a => a.FilterAttrs.ForEach(f => attrs.Add(f)));

            attrs.Distinct(new FilterAttributeComparer()).ToList().ForEach
                (a =>
            {
                var filter = FilterFactory.CreateInstance(a.GetType());
                if (filter != null)
                {
                    filters.Add(filter);
                }
            });

            Table[key] = filters;
            return(filters);
        }
Esempio n. 2
0
        public static TypeFilterInfo CreateInstance(Type importType, ExcelHeaderRow excelHeaderRow)
        {
            if (importType == null)
            {
                throw new ArgumentNullException("importDTOType");
            }

            if (excelHeaderRow == null)
            {
                throw new ArgumentNullException("excelHeaderRow");
            }

            var key = importType;

            if (Table[key] != null)
            {
                return((TypeFilterInfo)Table[key]);
            }

            TypeFilterInfo typeFilterInfo = new TypeFilterInfo()
            {
                PropertyFilterInfos = new List <PropertyFilterInfo>()
                {
                }
            };

            IEnumerable <PropertyInfo> props = importType.GetProperties().ToList().Where(p => p.IsDefined(typeof(ColNameAttribute)));

            props.ToList().ForEach(p =>
            {
                string colName = p.GetCustomAttribute <ColNameAttribute>().ColName;
                ExcelCol col   = excelHeaderRow.Cells.SingleOrDefault(c => c.ColName == colName);
                if (col != null)
                {
                    typeFilterInfo.PropertyFilterInfos.Add(
                        new PropertyFilterInfo
                    {
                        ColIndex    = col.ColIndex,
                        FilterAttrs = p.GetCustomAttributes <BaseFilterAttribute>()?.ToList()
                    });
                }
            });

            Table[key] = typeFilterInfo;

            return(typeFilterInfo);
        }
 /// <summary>
 /// 获取某单元格的某校验类型集合
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="col"></param>
 /// <param name="typeFilterInfo"></param>
 /// <returns></returns>
 public static List <T> GetFilterAttrs <T>(this ExcelDataCol col, TypeFilterInfo typeFilterInfo)
     where T : BaseFilterAttribute
 {
     return(typeFilterInfo.PropertyFilterInfos.SingleOrDefault(a => a.ColIndex == col.ColIndex)?.
            FilterAttrs?.Where(e => e.GetType() == typeof(T)).Cast <T>().ToList());
 }
 /// <summary>
 /// 获取某单元格的某校验特性
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="col"></param>
 /// <param name="typeAttrs"></param>
 /// <returns></returns>
 public static T GetFilterAttr <T>(this ExcelDataCol col, TypeFilterInfo typeFilterInfo)
     where T : BaseFilterAttribute
 {
     return((T)typeFilterInfo.PropertyFilterInfos.SingleOrDefault(a => a.ColIndex == col.ColIndex)?.
            FilterAttrs?.SingleOrDefault(e => e.GetType() == typeof(T)));
 }