【发布时间】:2021-03-08 22:16:14
【问题描述】:
我正在尝试使用 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() { }
}
这是一个错误还是我错过了什么?
【问题讨论】:
-
完整代码在第一个sn-p中。 dotnetfiddle 中的相同 sn-p。
-
我想我在这里使用的是最新版本(10.1.1)
-
我已经用没有
MultiPoint的repro 更新了这个问题。 -
嗯,是的,但是你确实需要地图和
ConstructUsing:) 而且你不能同时将 smth 映射为集合和 POCO。检查the execution plan。 -
我不太明白。是预期的吗?有没有其他选择?查看执行计划,并不清楚它在哪里抛出。 In 确实包含一个`$typeMapDestination = ($dest ??.New A.TestCollection(.Call System.Array.Empty()));` 这意味着
ConstructUsing似乎工作。
标签: c# automapper nettopologysuite