如何轻松合并两个具有不同数据结构的匿名对象?



我想合并这两个匿名对象:

var man1 = new {
name = new {
first = "viet"
},
age = 20
};
var man2 = new {
name = new {
last = "vo"
},
address = "123 street"
};

合二为一:

var man = new {
name = new {
first = "viet",
last = "vo"
},
age = 20,
address = "123 street"
};

我寻找解决方案,但没有发现任何聪明的东西。

将匿名对象转换为ExpandoObject它本质上是string键和object值的字典:

var man1Expando = man1.ToDynamic();
var man2Expando = man2.ToDynamic();
public static ExpandoObject ToDynamic(this object obj)
{
IDictionary<string, object> expando = new ExpandoObject();
foreach (var propertyInfo in obj.GetType().GetProperties())
{
var currentValue = propertyInfo.GetValue(obj);
if (propertyInfo.PropertyType.IsAnonymous())
{
expando.Add(propertyInfo.Name, currentValue.ToDynamic());
}
else
{
expando.Add(propertyInfo.Name, currentValue);
}
}
return expando as ExpandoObject;
}

我正在使用帮助程序扩展来确定类型是否为匿名类型:

public static bool IsAnonymous(this Type type)
{
return type.DeclaringType is null
&& type.IsGenericType
&& type.IsSealed
&& type.IsClass
&& type.Name.Contains("Anonymous");
}

然后,将两个生成的 expando 对象合并为一个,但以递归方式检查嵌套的 expando 对象:

var result = MergeDictionaries(man1Expando, man2Expando, overwriteTarget: true);
public static IDictionary<string, object> MergeDictionaries(
IDictionary<string, object> targetDictionary,
IDictionary<string, object> sourceDictionary,
bool overwriteTarget)
{
foreach (var pair in sourceDictionary)
{
if (!targetDictionary.ContainsKey(pair.Key))
{
targetDictionary.Add(pair.Key, sourceDictionary[pair.Key]);
}
else
{
if (targetDictionary[pair.Key] is IDictionary<string, object> innerTargetDictionary)
{
if (pair.Value is IDictionary<string, object> innerSourceDictionary)
{
targetDictionary[pair.Key] = MergeDictionaries(
innerTargetDictionary,
innerSourceDictionary,
overwriteTarget);
}
else
{
// What to do when target propety is nested, but source is not?
// Who takes precedence? Target nested property or source value?
if (overwriteTarget)
{
// Replace target dictionary with source value.
targetDictionary[pair.Key] = pair.Value;
}
}
}
else
{
if (pair.Value is IDictionary<string, object> innerSourceDictionary)
{
// What to do when target propety is not nested, but source is?
// Who takes precedence? Target value or source nested value?
if (overwriteTarget)
{
// Replace target value with source dictionary.
targetDictionary[pair.Key] = innerSourceDictionary;
}
}
else
{
// Both target and source are not nested.
// Who takes precedence? Target value or source value?
if (overwriteTarget)
{
// Replace target value with source value.
targetDictionary[pair.Key] = pair.Value;
}
}
}
}
}
return targetDictionary;
}

overwriteTarget参数决定合并时哪个对象优先。

使用代码:

var man1 = new
{
name = new
{
first = "viet",
},
age = 20,
};
var man2 = new
{
name = new
{
last = "vo",
},
address = "123 street",
};
var man1Expando = man1.ToDynamic();
var man2Expando = man2.ToDynamic();
dynamic result = MergeDictionaries(man1Expando, man2Expando, overwriteTarget: true);
Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));

结果是:

{
"name": {
"first": "viet",
"last": "vo"
},
"age": 20,
"address": "123 street"
}

请注意我如何将结果分配给dynamic。让编译器分配类型将使 expando 对象显示为IDictionary<string, object>。使用字典表示形式,不能像访问匿名对象那样访问属性:

var result = MergeDictionaries(man1Expando, man2Expando, overwriteTarget: true);
result.name; // ERROR

这就是为什么dynamic.使用dynamic您将丢失编译时检查,但将两个匿名对象合并为一个。你必须自己判断它是否适合你。

C# 语言中没有任何内置内容可以支持您的用例。因此,标题中的问题需要用"对不起,没有简单的方法">来回答。

我可以提供以下替代方案:

  1. 手动操作:

    var man = new {
    name = new {
    first = man1.name.first,
    last = man2.name.first
    },
    age = man1.age,
    address = man2.address
    };
    
  2. 对结果类型使用类而不是匿名类型(我们称之为CompleteMan)。然后,您可以

    • 创建一个新的实例var man = new CompleteMan();
    • 使用反射来收集你的"部分人"(man1man2)的属性和价值,
    • 将这些值分配给man的属性。

    从某种意义上说,这是"简单"的,因为实现将相当简单,但它仍然是大量的代码,并且您需要考虑嵌套类型(name)。

    如果你迫切地想避免非匿名类型,你可能使用一个空的匿名目标对象,但是创建这个对象(var man = new { name = new { first = (string)null, last = (string)null, ...)实际上并不比首先创建一个类少。

  3. 使用专用的动态数据结构而不是匿名 C# 类:

    • Newtonsoft JSON 库支持 JSON 对象的合并。
    • 字典也可以轻松合并。
    • ExpandoObject也可以轻松合并。

最新更新