/// <summary> /// 从类型所有的装饰特性中,获取某类型的第一个匹配特性 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="typeDecoratorInfo"></param> /// <param name="decoratorAttrType"></param> /// <returns></returns> public static T GetDecorateAttr <T>(this TypeDecoratorInfo typeDecoratorInfo) where T : BaseDecorateAttribute { var attr = typeDecoratorInfo.TypeDecoratorAttrs.SingleOrDefault(a => a.GetType() == typeof(T)); return(attr == null ? null : (T)attr); }
/// <summary> /// 从类型所有的装饰特性中,获取某类型的所有匹配特性 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="typeDecoratorInfo"></param> /// <param name="decoratorAttrType"></param> /// <returns></returns> public static List <T> GetDecorateAttrs <T>(this TypeDecoratorInfo typeDecoratorInfo) where T : BaseDecorateAttribute { var attrs = typeDecoratorInfo.TypeDecoratorAttrs.Where(a => a.GetType() == typeof(T)); return(attrs == null ? null : attrs.Cast <T>().ToList()); }
/// <summary> /// 获取装饰器特性集合 /// </summary> /// <typeparam name="T">装饰器特性类型</typeparam> /// <param name="typeDecoratorInfo">类型装饰器信息</param> public static List <T> GetDecoratorAttributes <T>(this TypeDecoratorInfo typeDecoratorInfo) where T : DecoratorAttributeBase { var attributes = typeDecoratorInfo.TypeDecorators.Where(x => x.GetType() == typeof(T)); return(attributes.Cast <T>().ToList()); }
/// <summary> /// 创建类型装饰器信息实例 /// </summary> /// <param name="type">类型</param> public static TypeDecoratorInfo CreateInstance(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (TypeDecoratorDict.ContainsKey(type)) { return(TypeDecoratorDict[type]); } var typeDecoratorInfo = new TypeDecoratorInfo(); // 全局装饰器特性 typeDecoratorInfo.TypeDecorators.AddRange(type.GetCustomAttributes <DecoratorAttributeBase>()); // 列装饰器特性 var props = type.GetProperties().Where(x => x.IsDefined(typeof(ColumnNameAttribute))).ToList(); for (int i = 0; i < props.Count; i++) { typeDecoratorInfo.PropertyDecoratorInfos.Add(new PropertyDecoratorInfo() { ColumnIndex = i, Decorators = props[i].GetCustomAttributes <DecoratorAttributeBase>().ToList() }); } TypeDecoratorDict[type] = typeDecoratorInfo; return(typeDecoratorInfo); }
/// <summary> /// 获取模板类的装饰器信息 /// </summary> /// <param name="exportType"></param> /// <returns></returns> public static TypeDecoratorInfo CreateInstance(Type exportType) { if (exportType == null) { throw new ArgumentNullException("importDTOType"); } var key = exportType; if (Table[key] != null) { return((TypeDecoratorInfo)Table[key]); } TypeDecoratorInfo typeDecoratorInfo = new TypeDecoratorInfo() { TypeDecoratorAttrs = new List <BaseDecorateAttribute>() { }, PropertyDecoratorInfos = new List <PropertyDecoratorInfo>() { } }; //全局装饰特性 typeDecoratorInfo.TypeDecoratorAttrs.AddRange(exportType.GetCustomAttributes <BaseDecorateAttribute>()); //列装饰特性 List <PropertyInfo> props = exportType.GetProperties().ToList().Where(p => p.IsDefined(typeof(ColNameAttribute))).ToList(); for (int i = 0; i < props.Count(); i++) { typeDecoratorInfo.PropertyDecoratorInfos.Add( new PropertyDecoratorInfo { ColIndex = i, DecoratorAttrs = props[i].GetCustomAttributes <BaseDecorateAttribute>()?.ToList() }); } Table[key] = typeDecoratorInfo; return(typeDecoratorInfo); }
/// <summary> /// 获取所有的装饰器 /// </summary> /// <returns></returns> private static List <IDecorator> GetDecorators <T>() where T : class, new() { List <IDecorator> decorators = new List <IDecorator>(); List <BaseDecorateAttribute> attrs = new List <BaseDecorateAttribute>(); TypeDecoratorInfo typeDecoratorInfo = TypeDecoratorInfoFactory.CreateInstance(typeof(T)); attrs.AddRange(typeDecoratorInfo.TypeDecoratorAttrs); typeDecoratorInfo.PropertyDecoratorInfos.ForEach(a => attrs.AddRange(a.DecoratorAttrs)); attrs.Distinct(new DecoratorAttributeComparer()).ToList().ForEach (a => { var decorator = DecoratorFactory.CreateInstance(a.GetType()); if (decorator != null) { decorators.Add(decorator); } }); return(decorators); }
/// <summary> /// 获取装饰器特性 /// </summary> /// <typeparam name="T">装饰器特性类型</typeparam> /// <param name="typeDecoratorInfo">类型装饰器信息</param> public static T GetDecoratorAttribute <T>(this TypeDecoratorInfo typeDecoratorInfo) where T : DecoratorAttributeBase { var attribute = typeDecoratorInfo.TypeDecorators.SingleOrDefault(x => x.GetType() == typeof(T)); return((T)attribute); }