EntityHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Data;
  9. using Microsoft.Extensions.Logging;
  10. namespace FMSAdmin.Helpers {
  11. public class EntityHelper {
  12. public static ILogger _logger;
  13. public EntityHelper() {
  14. }
  15. /*
  16. * target 오브젝트를 source 내용으로 Merge
  17. * @param target
  18. * @param source
  19. * @param exclude 제외할 필드
  20. * @param include 포함할 필드
  21. * @param nullMerge NULL Merge 여부
  22. * @param zeroListMerge List 0개 Item Merge 여부
  23. * */
  24. public static void Merge(object target, object source, string exclude = null, string include = null, bool nullMerge = false, bool zeroListMerge = false) {
  25. if (target.GetType() != source.GetType()) {
  26. new Exception("객체의 타입이 서로 다릅니다. (" + target.GetType() + "!=" + source.GetType() + ")");
  27. }
  28. Type type = target.GetType();
  29. foreach (var prop in type.GetProperties()) {
  30. if (!prop.CanWrite) continue;
  31. var val = prop.GetValue(source, null);
  32. if (!nullMerge) {
  33. if (val == null) continue;
  34. }
  35. if (!zeroListMerge) {
  36. IList list = val as IList;
  37. if (list != null) {
  38. if (list.Count == 0) continue;
  39. }
  40. }
  41. if (!string.IsNullOrEmpty(exclude)) {
  42. var excludeArray = exclude.Split(',');
  43. if (excludeArray.Contains(prop.Name)) continue;
  44. }
  45. if (!string.IsNullOrEmpty(include)) {
  46. var includeArray = include.Split(',');
  47. if (!includeArray.Contains(prop.Name)) continue;
  48. prop.SetValue(target, val, null);
  49. } else {
  50. try {
  51. prop.SetValue(target, val, null);
  52. } catch (Exception ex) {
  53. _logger.LogError("prop:" + prop.Name + ",val:" + val, ex);
  54. }
  55. }
  56. }
  57. }
  58. /*
  59. * target 오브젝트를 source 내용으로 Merge (Value만)
  60. * @param target
  61. * @param source
  62. * @param exclude 제외할 필드
  63. * @pagam include 포함할 필드
  64. * */
  65. public static void MergeValue(object target, object source, string exclude = null, string include = null) {
  66. if (target.GetType() != source.GetType()) {
  67. new Exception("객체의 타입이 서로 다릅니다.");
  68. }
  69. Type type = target.GetType();
  70. foreach (var prop in type.GetProperties()) {
  71. var val = prop.GetValue(source, null);
  72. if (val == null) continue;
  73. if (!prop.PropertyType.IsValueType && prop.PropertyType != typeof(string)) continue;
  74. if (!string.IsNullOrEmpty(exclude)) {
  75. var excludeArray = exclude.Split(',');
  76. if (excludeArray.Contains(prop.Name)) continue;
  77. }
  78. if (!string.IsNullOrEmpty(include)) {
  79. var includeArray = include.Split(',');
  80. if (!includeArray.Contains(prop.Name)) continue;
  81. prop.SetValue(target, val, null);
  82. } else {
  83. try {
  84. prop.SetValue(target, val, null);
  85. } catch (Exception ex) {
  86. _logger.LogError("prop:" + prop.Name + ",val:" + val, ex);
  87. }
  88. }
  89. }
  90. }
  91. public DataTable ToDataTable(IList list, string dataTableName = "") {
  92. DataTable dt = new DataTable(dataTableName);
  93. foreach (var prop in list[0].GetType().GetProperties()) {
  94. dt.Columns.Add(prop.Name);
  95. }
  96. foreach (object obj in list) {
  97. object[] fieldList = obj as object[];
  98. dt.Rows.Add(fieldList);
  99. }
  100. return dt;
  101. }
  102. }
  103. }