using System;
using ;
using ;
using ;
using ;
using ;
using ;
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 = ;
PropertyInfo[] queryPts = null;
_dynamicObjectProperties.TryGetValue(key, out queryPts);
if (queryPts == null)
{
queryPts = ();
_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 = ();
mergerPts = _type.("<>") ? _type.GetProperties() : GetObjectProperties<TTarget>();
var dynamicResult = new ExpandoObject() as IDictionary<string, Object>;
foreach (var p in targetPts)
{
var attributes = (typeof(IngorePropertyAttribute), true);
if (() != null) continue;
(, (s, null));
}
foreach (var p in mergerPts)
{
var attributes = (typeof(IngorePropertyAttribute), true);
if (() != null) continue;
(, (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 = ();
mergerPts = _type.("<>") ? _type.GetProperties() : GetObjectProperties<TTarget>();
var result = new List<IDictionary<string, Object>>();
(x =>
{
var dynamicResult = new ExpandoObject() as IDictionary<string, Object>;
foreach (var p in targetPts)
{
var attributes = (typeof(IngorePropertyAttribute), true);
if (() != null) continue;
(, (x, null));
}
foreach (var p in mergerPts)
{
var attributes = (typeof(IngorePropertyAttribute), true);
if (() != null) continue;
(, (t, null));
}
(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 = (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; }
}
}
}