gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:25:34 28 4
gpt4 key购买 nike

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

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 的.让编译器分配类型将使您得到显示为 IDictionary<string, object> 的 expando 对象。 .使用字典表示,您不能像访问匿名对象那样访问属性:

var result = MergeDictionaries(man1Expando, man2Expando, overwriteTarget: true);

result.name; // ERROR

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

关于c# - 如何轻松合并两个具有不同数据结构的匿名对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67946924/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com