public ModelDecorators(T val, TS decorators, FilterSetting filter = null) { Val = val; Decorators = decorators; PropertyDecorators = GetPropertyNames(typeof(T), PropertyDecorators, filter, "", null); }
private Dictionary <string, TS> GetPropertyNames(Type type, Dictionary <string, TS> propertyList, FilterSetting filter, string memberPath, Type callingType) { var reflectedPrpInfos = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); if (filter != null) { if (filter.Include) { reflectedPrpInfos = reflectedPrpInfos.Where(prp => prp.GetCustomAttributes <LimitDecorationToAttribute>().Any()).ToArray(); } else { reflectedPrpInfos = reflectedPrpInfos.Where(prp => !prp.GetCustomAttributes <ExcludeFromDecorationAttribute>().Any()).ToArray(); } } IList <PropertyInfo> prpInfos; if (callingType != null) { prpInfos = reflectedPrpInfos.Where(pi => !pi.PropertyType.Equals(callingType)).ToList(); } else { prpInfos = reflectedPrpInfos; } for (int i = 0; i < prpInfos.Count; i++) { if (!prpInfos[i].PropertyType.IsValueType && prpInfos[i].PropertyType != typeof(string)) { if (!typeof(IEnumerable).IsAssignableFrom(prpInfos[i].PropertyType) || prpInfos[i].PropertyType == typeof(string)) { propertyList.Add($"{memberPath}{prpInfos[i].Name}", new TS()); var descendingPropertyList = GetPropertyNames(prpInfos[i].PropertyType, new Dictionary <string, TS>(), filter, $"{memberPath}{prpInfos[i].Name}.", type); foreach (var prop in descendingPropertyList) { propertyList.Add(prop.Key, prop.Value); } } } else { propertyList.Add($"{memberPath}{prpInfos[i].Name}", new TS()); } } return(propertyList); }
/// <summary> /// Initialize method to start A Decorator Pipeline Fluent Definition /// </summary> /// <typeparam name="TI">The model type</typeparam> /// <typeparam name="TS">The decorator type</typeparam> /// <param name="decorators">Func delegate to create a decorator type</param> /// <param name="filter">Specify if the attribute FilterDecorator is used to filter the properties to decorate</param> /// <returns>WithDecorators delegate</returns> public static WithDecorators <TI, IModelDecorators <TI, TS> > Init <TI, TS>(Func <TS> decorators, FilterSetting filter = null) where TI : new() where TS : DecoratorsAbstract, new() { return(input => { return new ModelDecorators <TI, TS>(input, decorators(), filter); }); }