123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Data;
- using Microsoft.Extensions.Logging;
- namespace FMSAdmin.Helpers {
- public class EntityHelper {
- public static ILogger _logger;
- public EntityHelper() {
- }
- /*
- * target 오브젝트를 source 내용으로 Merge
- * @param target
- * @param source
- * @param exclude 제외할 필드
- * @param include 포함할 필드
- * @param nullMerge NULL Merge 여부
- * @param zeroListMerge List 0개 Item Merge 여부
- * */
- public static void Merge(object target, object source, string exclude = null, string include = null, bool nullMerge = false, bool zeroListMerge = false) {
- if (target.GetType() != source.GetType()) {
- new Exception("객체의 타입이 서로 다릅니다. (" + target.GetType() + "!=" + source.GetType() + ")");
- }
- Type type = target.GetType();
- foreach (var prop in type.GetProperties()) {
- if (!prop.CanWrite) continue;
- var val = prop.GetValue(source, null);
- if (!nullMerge) {
- if (val == null) continue;
- }
- if (!zeroListMerge) {
- IList list = val as IList;
- if (list != null) {
- if (list.Count == 0) continue;
- }
- }
- if (!string.IsNullOrEmpty(exclude)) {
- var excludeArray = exclude.Split(',');
- if (excludeArray.Contains(prop.Name)) continue;
- }
- if (!string.IsNullOrEmpty(include)) {
- var includeArray = include.Split(',');
- if (!includeArray.Contains(prop.Name)) continue;
- prop.SetValue(target, val, null);
- } else {
- try {
- prop.SetValue(target, val, null);
- } catch (Exception ex) {
- _logger.LogError("prop:" + prop.Name + ",val:" + val, ex);
- }
- }
- }
- }
- /*
- * target 오브젝트를 source 내용으로 Merge (Value만)
- * @param target
- * @param source
- * @param exclude 제외할 필드
- * @pagam include 포함할 필드
- * */
- public static void MergeValue(object target, object source, string exclude = null, string include = null) {
- if (target.GetType() != source.GetType()) {
- new Exception("객체의 타입이 서로 다릅니다.");
- }
- Type type = target.GetType();
- foreach (var prop in type.GetProperties()) {
- var val = prop.GetValue(source, null);
- if (val == null) continue;
- if (!prop.PropertyType.IsValueType && prop.PropertyType != typeof(string)) continue;
- if (!string.IsNullOrEmpty(exclude)) {
- var excludeArray = exclude.Split(',');
- if (excludeArray.Contains(prop.Name)) continue;
- }
- if (!string.IsNullOrEmpty(include)) {
- var includeArray = include.Split(',');
- if (!includeArray.Contains(prop.Name)) continue;
- prop.SetValue(target, val, null);
- } else {
- try {
- prop.SetValue(target, val, null);
- } catch (Exception ex) {
- _logger.LogError("prop:" + prop.Name + ",val:" + val, ex);
- }
- }
- }
- }
- public DataTable ToDataTable(IList list, string dataTableName = "") {
- DataTable dt = new DataTable(dataTableName);
- foreach (var prop in list[0].GetType().GetProperties()) {
- dt.Columns.Add(prop.Name);
- }
- foreach (object obj in list) {
- object[] fieldList = obj as object[];
- dt.Rows.Add(fieldList);
- }
- return dt;
- }
- }
- }
|