FileHelpers
FileHelpers copied to clipboard
FileHelperEngine does not work with ClassBuilder
This condition is always false when I use FileHelperEngine with ClassBuilder
FileHelperEngine.cs (line 419)
if (RecordInfo.RecordType.IsInstanceOfType(rec) == false) {
throw new BadUsageException("This engine works with record of type " +
RecordInfo.RecordType.Name + " and you use records of type " +
rec.GetType().Name);
}
I ran the following test ...
[Test]
public void WriteFileFixed()
{
var cb = new FixedLengthClassBuilder("SampleType");
cb.AddField("Field1", 8, typeof(DateTime));
cb.LastField.Converter.Kind = ConverterKind.Date;
cb.LastField.Converter.Arg1 = "ddMMyyyy";
cb.LastField.FieldNullValue = DateTime.Now;
cb.AddField("Field2", 3, typeof(string));
cb.LastField.AlignMode = AlignMode.Right;
cb.LastField.AlignChar = ' ';
cb.AddField("Field3", 3, typeof(int));
cb.LastField.AlignMode = AlignMode.Right;
cb.LastField.AlignChar = '0';
cb.LastField.TrimMode = TrimMode.Both;
mEngine = new FileHelperEngine(cb.CreateRecordClass());
var records = new List<SampleType>
{
new SampleType {
Field1 = new DateTime(2010, 3, 2),
Field2 = "field2.1",
Field3 = 1
},
new SampleType {
Field1 = new DateTime(2010, 4, 5),
Field2 = "field2.2",
Field3 = 2
},
new SampleType {
Field1 = new DateTime(2010, 6, 7),
Field2 = "field2.3",
Field3 = 3
}
};
mEngine.WriteFile(FileTest.Good.Test1.Path, records);
mEngine.ReadFile(FileTest.Good.Test1.Path);
Assert.AreEqual("SampleType", mEngine.RecordType.Name);
Assert.AreEqual(3, mEngine.RecordType.GetFields().Length);
Assert.AreEqual("Field1", mEngine.RecordType.GetFields()[0].Name);
}
...and the following exception occurred
WriteFileFixed [0:00.256] Failed: FileHelpers.BadUsageException : This engine works with record of type SampleType and you use records of type SampleType
Would anyone have a working example of building the layout dynamically, without being the test sample here from the repository?