如何使AutoMapper保持源代码为空?
new MapperConfiguration(cfg => cfg.CreateMap<MyModel, MyModel>()
.ForMember(m => m.prop1, opt => opt.AllowNull())
.ForMember(m => m.prop1, opt => opt.NullSubstitute(null))
.ForMember(m => m.prop1, opt => opt.MapFrom(s => s.prop1))
).CreateMapper();
prop1是可为空的,例如string[]
我总是得到该类型的默认值。
答案 0 :(得分:2)
null
目标值和configuration.AllowNullCollections = true;
configuration.AllowNullDestinationValues = true;
集合是不允许的。您可以在配置中进行设置:
AfterMap
您还可以通过new MapperConfiguration(cfg => cfg.CreateMap<MyModel, MyModel>()
.AfterMap( (s,d) => d.prop1 = s.prop1 == null ? null : d.prop1 );
配置强制执行此操作:
let dropping = false;
// indicates that the bomb is dropping right now. Do not drop a new bomb
//...
if (e.keyCode == 32) {
drop = true;
dropping = true;
setTimeout(function () { dropping = false; }, 1000);
// if 1 second has passed, reset the dropping variable, to allow another bomb to drop
}
//...
if (drop && !dropping) {
bomb = new Bomb(player.x - 8, player.y + 50, 16, 1)
bombs.push(bomb);
}
答案 1 :(得分:2)
尝试
oppia
答案 2 :(得分:2)
The automapper documentation的内容如下:
映射集合属性时,如果源值为null AutoMapper会将目标字段映射到一个空集合 而不是将目标值设置为null。这与 实体框架和框架设计准则的行为 相信C#引用,数组,列表,集合,字典和 IEnumerables绝不能为null。
您可以使用AllowNullCollections
属性进行更改:
Mapper.Initialize(cfg => {
cfg.AllowNullCollections = true;
cfg.CreateMap<Source, Destination>();
});