using Hotline.Share.Dtos.ExportWord; using Hotline.Share.Enums.ExportWord; using System.Reflection; namespace Hotline.Application.ExportWord { public static class IWordExportTemplateExtensions { public static IEnumerable GetReplacements(this T wordData) where T : IWordExportTemplate { List list = new List(); PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo propertyInfo in properties) { PlaceholderEntity placeholderEntity = new PlaceholderEntity(); string placeholder = (propertyInfo.IsDefined(typeof(PlaceholderAttribute)) ? propertyInfo.GetCustomAttribute().Placeholder.ToString() : ("{" + propertyInfo.Name + "}")); if (propertyInfo.PropertyType == typeof(string)) { placeholderEntity.Placeholder = placeholder; placeholderEntity.PlaceholderType = EPlaceholderType.Text; placeholderEntity.Data = new ExportText { Data = propertyInfo.GetValue(wordData)?.ToString() }; list.Add(placeholderEntity); } else if (propertyInfo.PropertyType == typeof(ExportTable)) { placeholderEntity.Placeholder = placeholder; placeholderEntity.PlaceholderType = EPlaceholderType.Table; placeholderEntity.Data = (IWordElement)propertyInfo.GetValue(wordData); list.Add(placeholderEntity); } else if (propertyInfo.PropertyType == typeof(ExportPicture)) { placeholderEntity.Placeholder = placeholder; placeholderEntity.PlaceholderType = EPlaceholderType.Picture; ExportPicture item = (ExportPicture)propertyInfo.GetValue(wordData); placeholderEntity.Pictures = new List { item }; list.Add(placeholderEntity); } else if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType)) { placeholderEntity.Placeholder = placeholder; placeholderEntity.PlaceholderType = EPlaceholderType.Picture; IEnumerable pictures = (IEnumerable)propertyInfo.GetValue(wordData); placeholderEntity.Pictures = pictures; list.Add(placeholderEntity); } else if (propertyInfo.PropertyType == typeof(ExportParagraph)) { placeholderEntity.Placeholder = placeholder; placeholderEntity.PlaceholderType = EPlaceholderType.Paragraph; placeholderEntity.Data = (IWordElement)propertyInfo.GetValue(wordData); list.Add(placeholderEntity); } else if (propertyInfo.PropertyType == typeof(ExportComplex)) { placeholderEntity.Placeholder = placeholder; placeholderEntity.PlaceholderType = EPlaceholderType.Complex; placeholderEntity.Data = (IWordElement)propertyInfo.GetValue(wordData); list.Add(placeholderEntity); } } return list; } } }