我有这个失败的代码:
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 具有相同的值时,我得到这个异常:
已添加具有相同密钥的项目。
如果他们有不同的价值观,一切都会奏效。我使用 AutoMapper 2.2.0。
我做错了什么还是一个错误?我该如何解决?