123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using Hotline.Share.Dtos.ExportWord;
- using Hotline.Share.Enums.ExportWord;
- using System.Reflection;
- namespace Hotline.Application.ExportWord
- {
- public static class IWordExportTemplateExtensions
- {
- public static IEnumerable<PlaceholderEntity> GetReplacements<T>(this T wordData) where T : IWordExportTemplate
- {
- List<PlaceholderEntity> list = new List<PlaceholderEntity>();
- PropertyInfo[] properties = typeof(T).GetProperties();
- foreach (PropertyInfo propertyInfo in properties)
- {
- PlaceholderEntity placeholderEntity = new PlaceholderEntity();
- string placeholder = (propertyInfo.IsDefined(typeof(PlaceholderAttribute)) ? propertyInfo.GetCustomAttribute<PlaceholderAttribute>().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<ExportPicture> { item };
- list.Add(placeholderEntity);
- }
- else if (typeof(IEnumerable<ExportPicture>).IsAssignableFrom(propertyInfo.PropertyType))
- {
- placeholderEntity.Placeholder = placeholder;
- placeholderEntity.PlaceholderType = EPlaceholderType.Picture;
- IEnumerable<ExportPicture> pictures = (IEnumerable<ExportPicture>)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;
- }
- }
- }
|