gpt4 book ai didi

c# - 使用 AutoMapper 映射相同值时出错

转载 作者:太空狗 更新时间:2023-10-29 23:00:46 27 4
gpt4 key购买 nike

我有这个失败的代码:

internal class Program
{
private static void Main()
{
Mapper.CreateMap<SourceFoo, TargetFoo>();
Mapper.CreateMap<string, Stuff>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.IgnoreMe, opt => opt.Ignore());

var source = new SourceFoo
{
Stuff = "a",
Stuff2 = "a"
};
var target = new TargetFoo
{
Stuff = new Stuff(),
Stuff2 = new Stuff()
};
Mapper.Map(source, target);

Console.WriteLine(target.Stuff.Value);
Console.WriteLine(target.Stuff2.Value);
Console.ReadLine();
}
}

public class SourceFoo
{
public string Stuff { get; set; }

public string Stuff2 { get; set; }
}

public class TargetFoo
{
public Stuff Stuff { get; set; }

public Stuff Stuff2 { get; set; }
}

public class Stuff
{
public string Value { get; set; }
public bool IgnoreMe { get; set; }
}

当 Stuff/Stuff2 具有相同的值时,我得到这个异常:

An item with the same key has already been added.

如果它们具有不同的值,则一切正常。我使用 AutoMapper 2.2.0。

我做错了什么还是错误?我该如何解决?

最佳答案

这是一个错误。

现在您可以安装 2.1.267 版本了。

NuGet包管理器控制台命令:

Uninstall-Package AutoMapper -Force

Install-Package AutoMapper -Version 2.1.267

检查此链接:

https://github.com/AutoMapper/AutoMapper/issues/250

并且在里程碑中:2.2.1 问题 #270

关于c# - 使用 AutoMapper 映射相同值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767777/

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