【问题标题】:NetTopologySuite.Geometries.MultiPoint needs to have a constructor with 0 argsNetTopologySuite.Geometries.MultiPoint 需要有一个 0 args 的构造函数
【发布时间】: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


【解决方案1】:

我认为这是因为您的 MultiPoint 为空。使用 Nettopologysuite 多点(内部的点不能为空)。

MultiPoint mp1 = null;

MultiPoint mp1 = NetTopologySuite.Geometries.MultiPoint.Empty;

【讨论】:

  • 最后我使用了MultiPoint.Empty,但我不明白为什么 null 没有映射到 null。
猜你喜欢
  • 2011-06-25
  • 2016-10-01
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 2023-04-09
  • 2020-08-26
相关资源
最近更新 更多