How can I merge two anonymous types, so that the result contains the properties of both source objects?
var source1 = new
{
foo = \"foo\",
bar = \"ba
The following works in .NET 3.5 (and probably 2.0 as well). I modified davehauser's answer.
public static object MergeJsonData(object item1, object item2)
{
if (item1 == null || item2 == null)
return item1 ?? item2 ?? new object();
var result = new Dictionary();
foreach (System.Reflection.PropertyInfo fi in item1.GetType().GetProperties().Where(x => x.CanRead))
{
var Value = fi.GetValue(item1, null);
result[fi.Name] = Value;
}
foreach (System.Reflection.PropertyInfo fi in item2.GetType().GetProperties().Where(x => x.CanRead))
{
var Value = fi.GetValue(item2, null);
result[fi.Name] = Value;
}
return result;
}