【发布时间】:2020-07-18 04:52:09
【问题描述】:
我正在使用 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;
}
【问题讨论】:
-
这有点道理,因为您可以用值填充数组元素。
-
在 10.0 中,所有没有 setter 的集合属性都将被默认映射。
标签: c# automapper