我正在尝试使用Automapper 映射NetTopologySuite.Geometries.MultiPoint但我不断收到错误消息System.ArgumentException: NetTopologySuite.Geometries.MultiPoint needs to have a constructor with 0 args or only optional args
。
var config = new MapperConfiguration(cfg => {});
var mapper = config.CreateMapper();
MultiPoint mp1 = null;
MultiPoint mp2 = mapper.Map<MultiPoint>(mp1); // throws
实际上,这种类型没有带 0 个参数的构造函数。我试过指定如何实例化类型:
new MapperConfiguration(cfg => {
cfg.CreateMap<MultiPoint, MultiPoint>()
.ConstructUsing(mp => new MultiPoint((Point[])mp.Geometries));
});
同样的错误。为了用更简单的代码重现,我创建了一个没有 0 args 构造函数的类。
var config = new MapperConfiguration(cfg => { });
var mapper = config.CreateMapper();
TestCollection tc1 = null;
TestCollection tc2 = mapper.Map<TestCollection>(tc1); // throws
class Test
{
}
class TestCollection : IEnumerable<Test>
{
public TestCollection(Test[] tests) => Tests = tests;
public Test[] Tests { get; set; }
public IEnumerator<Test> GetEnumerator() => new TestCollectionEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
class TestCollectionEnumerator : IEnumerator<Test>
{
object IEnumerator.Current => Current;
public Test Current { get; }
public bool MoveNext() => false;
public void Reset() { }
public void Dispose() { }
}
这是一个错误还是我错过了什么?