我正在定义源类和目标类之间的映射:
Mapper.CreateMap<Source, Target>();
如果 Source
上的某个属性设置为特定值,我希望映射返回 null
:
// If Source.IsValid = false I want the mapping to return null
Source s = new Source { IsValid = false };
Target t = Mapper.Map<Target>(s);
Assert.IsNull(t);
如何配置 AutoMapper 来实现这一点?
最佳答案
您可以这样定义您的映射:
Mapper.CreateMap<Source, Target>().TypeMap
.SetCondition(r => ((Source)r.SourceValue).IsValid);
关于c# - 当 source 上的属性为 false 时是否可以映射到 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31016357/