【问题标题】:No public parameterless constructor found when using Interface使用接口时找不到公共无参数构造函数
【发布时间】:2019-07-04 16:44:04
【问题描述】:

读取我的 CSV 并使用类映射时,我收到一条错误消息,提示“未找到公共无参数构造函数”。我 100% 确定这是因为我的 ClassMap 中的类有一个属性,在我的例子中是一个接口 IAddress。有没有办法将此属性映射到 Address 的类实现?

我曾尝试使用类似 References(m => m.Address, mappings); 之类的参考地图;

这是我的代码(为简洁起见,省略了一些属性):


  public class Customer
  {
    public int Id { get; set; }

    public IAddress CurrentAddress { get; set; }

    public Customer()
    {
    }
  }

  public sealed class CustomerMap : ClassMap<Customer>
  {
      public CustomerMap(Dictionary<string, string> mappings)
      {
          References<AddressMappings>(m => m.CurrentAddress, mappings);
      }
  }

 public class AddressMappings : ClassMap<IAddress>
 {
     public AddressMappings(Dictionary<string, string> mappings)
     {
         Map(m => m.FlatNumber).Name(mappings["FlatNumber"]);
         Map(m => m.PropertyNumber).Name(mappings["PropertyNumber"]);
         Map(m => m.PropertyName).Name(mappings["PropertyName"]);
         Map(m => m.AddressLine1).Name(mappings["AddressLine1"]);
         Map(m => m.AddressLine2).Name(mappings["AddressLine2"]);
         Map(m => m.AddressLine3).Name(mappings["AddressLine3"]);
         Map(m => m.Town).Name(mappings["Town"]);
         Map(m => m.City).Name(mappings["City"]);
         Map(m => m.Ward).Name(mappings["Ward"]);
         Map(m => m.Parish).Name(mappings["Parish"]);
         Map(m => m.County).Name(mappings["County"]);
         Map(m => m.Country).Name(mappings["Country"]);
         Map(m => m.Postcode).Name(mappings["Postcode"]);
     }
 }

using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader))
{
    csv.Configuration.Delimiter = ",";
    var mappingObject = new CustomerMap(mappings);
    csv.Configuration.RegisterClassMap(mappingObject);
    var records = csv.GetRecords<Customer>();
    return records?.ToList();
}


【问题讨论】:

    标签: csvhelper


    【解决方案1】:

    我认为这可能对你有用。

    public sealed class CustomerMap : ClassMap<Customer>
    {
        public CustomerMap(Dictionary<string, string> mappings)
        {
            Map(m => m.CurrentAddress).ConvertUsing(row => 
                new Address {
                    FlatNumber = row.GetField<int>(mappings["FlatNumber"]),
                    PropertyNumber = row.GetField<int>(mappings["PropertyNumber"]),
                    AddressLine1 = row.GetField(mappings["AddressLine1"]),
                    AddressLine2 = row.GetField(mappings["AddressLine2"]),
                    AddressLine3 = row.GetField(mappings["AddressLine3"]),
                    Town = row.GetField(mappings["Town"]),
                    City = row.GetField(mappings["City"]),
                    Ward = row.GetField(mappings["Ward"]),
                    Parish = row.GetField(mappings["Parish"]),
                    County = row.GetField(mappings["County"]),
                    Country = row.GetField(mappings["Country"]),
                    Postcode = row.GetField(mappings["Postcode"])
                });
        }
    }
    

    【讨论】:

    • 这看起来很有希望,我试试看,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2020-09-15
    • 2011-02-08
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多