[英]Create one object from properties and add to collection of destination with automapper
[英]In AutoMapper, read only collection properties on the destination type are not automatically ignored
我正在使用 AutoMapper v9.0.0。 由于非集合只读属性(仅具有 getter 的属性)在目标类型上被自动忽略,我期望集合属性也是如此。 但是,断言映射器配置时会引发异常。 为了进一步澄清,请参见下面的代码。 这是设计使然还是错误?
class Program
{
static void Main(string[] args)
{
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<SourceType, DestinationTypeWithGetterOnly>();
});
//valid
config.AssertConfigurationIsValid();
config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<SourceType, DestinationTypeWithGetterOnlyArray>();
});
//throws exception Unmapped properties: CalculatedArray
config.AssertConfigurationIsValid();
}
}
class SourceType { }
class DestinationTypeWithGetterOnlyArray
{
public string[] CalculatedArray => new string[0];
}
class DestinationTypeWithGetterOnly
{
public string CalculatedProperty => string.Empty;
}
在 github 上引用@LucianBargaoanu
按设计。 您可以使用ForAllMaps
和ForAllMembers
或ForAllPropertyMaps
来实现您想要的。 或者可能ShouldMapField
\ ShouldMapProperty
。 例如,
cfg.ShouldMapProperty = property => property.CanWrite;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.