例如,我有两行的下一个文件test.txt:
Type00007P 008 PPL
Type00230J 190 1
代码中的下一个类使用Rhino-ETL Nuget在我的数据库中插入这些行:
public class Type00007P
{
[FieldFixedLength(10)]
[FieldTrim(TrimMode.Both)]
public string TypeControl;
[FieldFixedLength(3)]
[FieldTrim(TrimMode.Both)]
public int Id;
[FieldFixedLength(3)]
[FieldTrim(TrimMode.Both)]
public string Name;
}
public class Type00230J
{
[FieldFixedLength(10)]
[FieldTrim(TrimMode.Both)]
public string TypeControl;
[FieldFixedLength(3)]
[FieldTrim(TrimMode.Both)]
public int Id;
[FieldFixedLength(1)]
[FieldTrim(TrimMode.Both)]
public bool Calculated;
}如果我使用下面的代码提取行,我无法区分同一文件中的行Type00007P和行Type00230J:
public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
using (FileEngine file = FluentFile.For<Type00007P>().From(FilePath))
{
foreach (object obj in file)
{
yield return Row.FromObject(obj);
}
}
}那么,如何读取第一个固定字段来获取RowType,然后用正确的类处理整行呢?
致以问候!
发布于 2018-08-17 00:46:51
您可以使用MultiRecordEngine。文档是here。
var engine = new MultiRecordEngine(
typeof(Type00007P),
typeof(Type00230J));
engine.RecordSelector = new RecordTypeSelector(CustomSelector);然后定义一个CustomSelector。
private Type CustomSelector(MultiRecordEngine engine, string recordLine)
{
if (recordLine.Length == 0)
return null;
if (recordLine.StartsWith("Type00007P")))
return typeof(Type00007P);
else
return typeof(Type00230J);
}https://stackoverflow.com/questions/51663193
复制相似问题