0

我需要修补一个字典的值与另一个动态添加新键。我需要保存新密钥并替换现有密钥才能完成补丁功能。必须有我无法访问的现有映射。或者我应该写什么映射配置文件?

[Test]
public void DictionaryMapping()
{
    // Arrange
    var write = new Dictionary<string, object>
    {
        { "Key1", 100 },
        { "Key2", true }
    };
            var patch = new Dictionary<string, object>
    {
        { "Key2", false },
        { "Key3", "Additional"}
    };

    // Act
    Mapper.Map(patch, write);

    // Assert
    Assert.That(write["Key1"], Is.EqualTo(100));
    Assert.That(write["Key2"], Is.EqualTo(false));
    Assert.That(write["Key3"], Is.EqualTo("Additional"));
}
4

1 回答 1

4

您可以像这样合并字典:

foreach(var keyValue in patch)
    write[keyValue.Key] = keyValue.Value;

它将覆盖现有的并为不存在的创建新的。

于 2013-08-26T12:22:00.783 回答