using JNPF.DependencyInjection; using System.Collections.Concurrent; using System.Dynamic; using System.Reflection; using Newtonsoft.Json; namespace JNPF.Common.Extension; /// /// 多线程下的字典辅助扩展方法. /// [SuppressSniffer] public static class ConcurrentDictionaryExtensions { private static readonly ConcurrentDictionary _dynamicObjectProperties = new ConcurrentDictionary(); /// /// 获取object属性. /// /// 对象类型. /// private static PropertyInfo[] GetObjectProperties() { 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; } /// /// 合并2个对象. /// /// 对象1类型. /// 对象2类型. /// 对象1实例. /// 对象2实例. /// 合并后的动态对象. public static IDictionary MergerObject(TSource s, TTarget t) { var targetPts = GetObjectProperties(); PropertyInfo[] mergerPts = null; var _type = t.GetType(); mergerPts = _type.Name.Contains("<>") ? _type.GetProperties() : GetObjectProperties(); var dynamicResult = new ExpandoObject() as IDictionary; foreach (var p in targetPts) { var attributes = p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true); if (attributes.FirstOrDefault() != null) continue; dynamicResult.Add(p.Name, p.GetValue(s, null)); } foreach (var p in mergerPts) { var attributes = p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true); if (attributes.FirstOrDefault() != null) continue; dynamicResult.Add(p.Name, p.GetValue(t, null)); } return dynamicResult; } /// /// 对象2值赋值至对象1内. /// /// 对象1类型. /// 对象2类型. /// 对象1实例. /// 对象2实例. /// 合并后的动态对象. public static IDictionary AssignmentObject(TSource s, TTarget t) { var targetPts = GetObjectProperties(); PropertyInfo[] mergerPts = null; var _type = t.GetType(); mergerPts = _type.Name.Contains("<>") ? _type.GetProperties() : GetObjectProperties(); var dynamicResult = new ExpandoObject() as IDictionary; foreach (var p in targetPts) { var attributes = p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true); if (attributes.FirstOrDefault() != null) continue; dynamicResult.Add(p.Name, p.GetValue(s, null)); } foreach (var p in mergerPts) { var attributes = p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true); if (attributes.FirstOrDefault() != null) continue; dynamicResult[p.Name] = p.GetValue(t, null); } return dynamicResult; } /// /// 合并2个对象. /// var result = MergerListObject(kk, new { p = new KeyValue2() { key2 = "dadad", key3 = "biubiu" } }); /// /// 对象1类型. /// 对象2类型. /// 对象1实例. /// 对象2实例. /// 合并后的动态对象. public static List> MergerListObject(List s, TTarget t) { var targetPts = GetObjectProperties(); PropertyInfo[] mergerPts = null; var _type = t.GetType(); mergerPts = _type.Name.Contains("<>") ? _type.GetProperties() : GetObjectProperties(); var result = new List>(); s.ForEach(x => { var dynamicResult = new ExpandoObject() as IDictionary; foreach (var p in targetPts) { var attributes = p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true); if (attributes.FirstOrDefault() != null) continue; dynamicResult.Add(p.Name, p.GetValue(x, null)); } foreach (var p in mergerPts) { var attributes = p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true); if (attributes.FirstOrDefault() != null) continue; dynamicResult.Add(p.Name, p.GetValue(t, null)); } result.Add(dynamicResult); }); return result; } }