Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileHelperEngine does not work with ClassBuilder #293

Open
Tabgyn opened this issue May 3, 2018 · 0 comments
Open

FileHelperEngine does not work with ClassBuilder #293

Tabgyn opened this issue May 3, 2018 · 0 comments
Labels

Comments

@Tabgyn
Copy link

Tabgyn commented May 3, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants