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; }
}
}
}
C# 合并实体对象
最新推荐文章于 2024-07-05 18:29:01 发布