【发布时间】:2021-05-06 11:50:45
【问题描述】:
假设我有这样的数据结构:
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 列?
【问题讨论】:
-
这里的问题不是嵌套类 - 这是可行的,它是
Bar被重用,所以你不能将Bar.Value映射到 2 个不同的列(即使它是 @987654335 的 2 个单独实例@)。如果你可以有 2 个不同的课程,我认为这是可能的(例如BarA和BarB)。 -
那是不可能的。我不控制用户输入什么类型,所以两个
Bars 是非常现实的。 -
我认为你仅限于reading by hand然后
-
谢谢。我认为这意味着我应该要求它成为一个功能。对我来说这听起来完全不可能。