public static object StoredToApi(Type type, object?o, ICustomPropertyConfigurationProvider?customPropertyConfigurationProvider) { if (o == null) { return(null); } var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); if (PropertyHelpers.IsSimpleType(type) || type == typeof(byte[]) || !properties.Any()) { return(o); } var result = new Dictionary <string, object?>(); foreach (var propertyInfo in properties) { var propertyValue = propertyInfo.GetValue(o); var customPropertyConfiguration = customPropertyConfigurationProvider?.TryGetConfiguration(o, propertyInfo); var resolvedProperty = customPropertyConfiguration != null?customPropertyConfiguration.StoredToApi(propertyValue) : propertyValue; result[propertyInfo.Name] = resolvedProperty; } return(result); }
public DownloadFileStream(Type type, object?[] objects, string[] excludedFields, ICustomPropertyConfigurationProvider customPropertyConfigurationProvider, int downloadLimit) { var properties = new List <string>(); var getters = new List <Func <object?, object?> >(); PropertyHelpers.BuildGettersForProperties(type, "", x => x, properties, getters, customPropertyConfigurationProvider); var excludedIndices = properties.Select((x, i) => (x, i)).Where(x => excludedFields.Contains(x.x)).Select(x => x.i).ToArray(); formatter = new CsvFormatter( objects, properties.Where((x, i) => !excludedIndices.Contains(i)).ToArray(), getters.Where((x, i) => !excludedIndices.Contains(i)).ToArray() ); }
public byte[] GetRows(int start, int end) { var sb = new StringBuilder(); end = Math.Min(end, data.Length); for (var i = start; i < end; i++) { for (var j = 0; j < getters.Length; j++) { sb.Append(FormatElement(PropertyHelpers.ToString(getters[j], data[i]))); sb.Append(j < getters.Length - 1 ? ';' : '\n'); } } return(Encoding.UTF8.GetBytes(sb.ToString())); }
public static object StoredToApiDeep(object?o, ICustomPropertyConfigurationProvider?customPropertyConfigurationProvider) { if (o == null) { return(null); } if (o is IDictionary dictionary) { return(dictionary.Keys.Cast <object>().ToDictionary( k => StoredToApiInternal(k, customPropertyConfigurationProvider), k => StoredToApiInternal(dictionary[k], customPropertyConfigurationProvider) )); } var type = o.GetType(); if (!PropertyHelpers.IsSimpleType(type) && type != typeof(byte[]) && o is IEnumerable enumerable) { return(enumerable.Cast <object>().Select(x => StoredToApiInternal(x, customPropertyConfigurationProvider)).ToArray()); } var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); if (PropertyHelpers.IsSimpleType(type) || type == typeof(byte[]) || !properties.Any()) { return(o); } var result = new Dictionary <string, object>(); foreach (var propertyInfo in properties) { var propertyValue = propertyInfo.GetValue(o); var customPropertyConfiguration = customPropertyConfigurationProvider?.TryGetConfiguration(o, propertyInfo); var resolvedProperty = customPropertyConfiguration != null?customPropertyConfiguration.StoredToApi(propertyValue) : propertyValue; result[propertyInfo.Name] = StoredToApiDeep(resolvedProperty, customPropertyConfigurationProvider); } return(result); }