C# 合并实体对象

using System;
using System.Linq;
using System.Dynamic;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.Concurrent;
using Newtonsoft.Json;

namespace Common
{
    /// <summary>
    /// 合并对象
    /// </summary>
    public class ConcurrentDictionaryHelper
    {
        private static readonly ConcurrentDictionary<RuntimeTypeHandle, PropertyInfo[]>
           _dynamicObjectProperties = new ConcurrentDictionary<RuntimeTypeHandle, PropertyInfo[]>();

        private static PropertyInfo[] GetObjectProperties<T>()
        {
            var type = typeof(T);
            var key = type.TypeHandle;
            PropertyInfo[] queryPts = null;

            _dynamicObjectProperties.TryGetValue(key, out queryPts);

            if (queryPts == null)
            {
                queryPts = type.GetProperties();
                _dynamicObjectProperties.TryAdd(key, queryPts);
            }
            return queryPts;
        }
        /// <summary>
        /// 合并2个对象
        /// </summary>
        /// <typeparam name="TSource">对象1类型</typeparam>
        /// <typeparam name="TTarget">对象2类型</typeparam>
        /// <param name="s">对象1实例</param>
        /// <param name="t">对象2实例</param>
        /// <returns>合并后的动态对象</returns>
        public static IDictionary<string, Object> MergerObject<TSource, TTarget>(TSource s, TTarget t)
        {
            var targetPts = GetObjectProperties<TSource>();

            PropertyInfo[] mergerPts = null;
            var _type = t.GetType();
            mergerPts = _type.Name.Contains("<>") ? _type.GetProperties() : GetObjectProperties<TTarget>();

            var dynamicResult = new ExpandoObject() as IDictionary<string, Object>;

            foreach (var p in targetPts)
            {
                var attributes = p.GetCustomAttributes(typeof(IngorePropertyAttribute), true);
                if (attributes.FirstOrDefault() != null) continue;

                dynamicResult.Add(p.Name, p.GetValue(s, null));
            }
            foreach (var p in mergerPts)
            {
                var attributes = p.GetCustomAttributes(typeof(IngorePropertyAttribute), true);
                if (attributes.FirstOrDefault() != null) continue;

                dynamicResult.Add(p.Name, p.GetValue(t, null));
            }

            return dynamicResult;
        }
        /// <summary>
        /// 合并2个对象
        /// </summary>
        /// <typeparam name="TSource">对象1类型</typeparam>
        /// <typeparam name="TTarget">对象2类型</typeparam>
        /// <param name="s">对象1实例</param>
        /// <param name="t">对象2实例</param>
        /// <returns>合并后的动态对象</returns>
        public static List<IDictionary<string, Object>> MergerListObject<TSource, TTarget>(List<TSource> s, TTarget t)
        {
            var targetPts = GetObjectProperties<TSource>();

            PropertyInfo[] mergerPts = null;
            var _type = t.GetType();
            mergerPts = _type.Name.Contains("<>") ? _type.GetProperties() : GetObjectProperties<TTarget>();

            var result = new List<IDictionary<string, Object>>();

            s.ForEach(x =>
            {
                var dynamicResult = new ExpandoObject() as IDictionary<string, Object>;

                foreach (var p in targetPts)
                {
                    var attributes = p.GetCustomAttributes(typeof(IngorePropertyAttribute), true);
                    if (attributes.FirstOrDefault() != null) continue;

                    dynamicResult.Add(p.Name, p.GetValue(x, null));
                }

                foreach (var p in mergerPts)
                {
                    var attributes = p.GetCustomAttributes(typeof(IngorePropertyAttribute), true);
                    if (attributes.FirstOrDefault() != null) continue;

                    dynamicResult.Add(p.Name, p.GetValue(t, null));
                }

                result.Add(dynamicResult);
            });

            return result;
        }


        /// <summary>
        /// 动态输出时忽略此标记的属性
        /// </summary>
        public class IngorePropertyAttribute : Attribute
        {
        }

        /// <summary>
        /// 测试方法
        /// </summary>
        /// <returns></returns>
        public static string Test()
        {
            //合并对象
            List<KeyValue> kk = new List<KeyValue>
            {
                new KeyValue{key="aaa", value="111",value2="ffff"},
                new KeyValue{key="bbb", value="222",value2="ffff"},
                new KeyValue{key="ccc", value="333",value2="ffff"},
                new KeyValue{key="ddd", value="444",value2="ffff"}
               };
            var result = MergerListObject<KeyValue, dynamic>(kk, new { p = new KeyValue2() { key2 = "dadad", key3 = "biubiu" } });
            var json = JsonConvert.SerializeObject(result);

            return json;
        }

        public class KeyValue2
        {
            public string key2 { get; set; }
            public string key3 { get; set; }
        }
        public class KeyValue
        {
            public string key { get; set; }
            public string value { get; set; }
            [IngoreProperty]
            public string value2 { get; set; }
        }


    }
}

转载:https://blog.csdn.net/Joyhen/article/details/50314977

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值