IWordExportTemplateExtensions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Hotline.Share.Dtos.ExportWord;
  2. using Hotline.Share.Enums.ExportWord;
  3. using System.Reflection;
  4. namespace Hotline.Application.ExportWord
  5. {
  6. public static class IWordExportTemplateExtensions
  7. {
  8. public static IEnumerable<PlaceholderEntity> GetReplacements<T>(this T wordData) where T : IWordExportTemplate
  9. {
  10. List<PlaceholderEntity> list = new List<PlaceholderEntity>();
  11. PropertyInfo[] properties = typeof(T).GetProperties();
  12. foreach (PropertyInfo propertyInfo in properties)
  13. {
  14. PlaceholderEntity placeholderEntity = new PlaceholderEntity();
  15. string placeholder = (propertyInfo.IsDefined(typeof(PlaceholderAttribute)) ? propertyInfo.GetCustomAttribute<PlaceholderAttribute>().Placeholder.ToString() : ("{" + propertyInfo.Name + "}"));
  16. if (propertyInfo.PropertyType == typeof(string))
  17. {
  18. placeholderEntity.Placeholder = placeholder;
  19. placeholderEntity.PlaceholderType = EPlaceholderType.Text;
  20. placeholderEntity.Data = new ExportText
  21. {
  22. Data = propertyInfo.GetValue(wordData)?.ToString()
  23. };
  24. list.Add(placeholderEntity);
  25. }
  26. else if (propertyInfo.PropertyType == typeof(ExportTable))
  27. {
  28. placeholderEntity.Placeholder = placeholder;
  29. placeholderEntity.PlaceholderType = EPlaceholderType.Table;
  30. placeholderEntity.Data = (IWordElement)propertyInfo.GetValue(wordData);
  31. list.Add(placeholderEntity);
  32. }
  33. else if (propertyInfo.PropertyType == typeof(ExportPicture))
  34. {
  35. placeholderEntity.Placeholder = placeholder;
  36. placeholderEntity.PlaceholderType = EPlaceholderType.Picture;
  37. ExportPicture item = (ExportPicture)propertyInfo.GetValue(wordData);
  38. placeholderEntity.Pictures = new List<ExportPicture> { item };
  39. list.Add(placeholderEntity);
  40. }
  41. else if (typeof(IEnumerable<ExportPicture>).IsAssignableFrom(propertyInfo.PropertyType))
  42. {
  43. placeholderEntity.Placeholder = placeholder;
  44. placeholderEntity.PlaceholderType = EPlaceholderType.Picture;
  45. IEnumerable<ExportPicture> pictures = (IEnumerable<ExportPicture>)propertyInfo.GetValue(wordData);
  46. placeholderEntity.Pictures = pictures;
  47. list.Add(placeholderEntity);
  48. }
  49. else if (propertyInfo.PropertyType == typeof(ExportParagraph))
  50. {
  51. placeholderEntity.Placeholder = placeholder;
  52. placeholderEntity.PlaceholderType = EPlaceholderType.Paragraph;
  53. placeholderEntity.Data = (IWordElement)propertyInfo.GetValue(wordData);
  54. list.Add(placeholderEntity);
  55. }
  56. else if (propertyInfo.PropertyType == typeof(ExportComplex))
  57. {
  58. placeholderEntity.Placeholder = placeholder;
  59. placeholderEntity.PlaceholderType = EPlaceholderType.Complex;
  60. placeholderEntity.Data = (IWordElement)propertyInfo.GetValue(wordData);
  61. list.Add(placeholderEntity);
  62. }
  63. }
  64. return list;
  65. }
  66. }
  67. }