如何在运行时为嵌套属性构建类映射

How to build a classmap at runtime for nested properties

假设我有这样的数据结构:

public class Foo
{
   public Bar A {get;set;}
   public Bar B {get;set;}
   public int C {get;set;}
}
public class Bar
{
   public int Value {get;set;}
}

和一个包含内容

的 CSV 文件
Column1,Column2,Column3
0,1,2
3,4,5

我现在想将 Column1 映射到 A.Value,将 Column2 映射到 B.Value,将 Column3 映射到 C。 我仅限于运行时映射。 对于 Column3 -> C,我可以写

var type = typeof(Foo);
var customMap = Activator.CreateInstance(typeof(DefaultClassMap<>).MakeGenericType(type)) as ClassMap;
customMap.Map(type, type.GetProperty("C")).Name("Column3");
csv_reader.Context.RegisterClassMap(customMap);

如何映射第 1 列和第 2 列?

目前你可以做到这一点。

void Main()
{
    var s = new StringBuilder();
    s.Append("Column1,Column2,Column3\r\n");
    s.Append("0,1,2\r\n");
    s.Append("3,4,5\r\n");
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
    };
    using (var reader = new StringReader(s.ToString()))
    using (var csv = new CsvReader(reader, config))
    {
        var fooType = typeof(Foo);
        var barType = typeof(Bar);
        
        var fooMapType = typeof(DefaultClassMap<>).MakeGenericType(fooType);
        var barMapType = typeof(DefaultClassMap<>).MakeGenericType(barType);
        
        var map = (ClassMap)ObjectResolver.Current.Resolve(fooMapType);
        map.Map(fooType, fooType.GetProperty("C")).Name("Column3");
        map.References(barMapType, fooType.GetProperty("A")).Data.Mapping.Map(barType, barType.GetProperty("Value")).Name("Column1");
        map.References(barMapType, fooType.GetProperty("B")).Data.Mapping.Map(barType, barType.GetProperty("Value")).Name("Column2");

        csv.Context.RegisterClassMap(map);
        csv.GetRecords<Foo>().ToList().Dump();
    }
}

private class Foo
{
    public Bar A { get; set; }
    public Bar B { get; set; }
    public int C { get; set; }
}

public class Bar
{
    public int Value { get; set; }
}

我正在研究如何让想要在运行时创建地图的人更容易做到这一点。