Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using FileHelpers;
- using NUnit.Framework;
- namespace ConsoleApplication8
- {
- class Program
- {
- [DelimitedRecord(",")]
- public class Format1
- {
- public string Field1;
- public string Field2;
- public string Field3;
- public string Field4;
- }
- [DelimitedRecord(",")]
- public class Format2
- {
- public string Field1;
- public string Field4;
- }
- static Type CustomSelector(MultiRecordEngine engine, string record)
- {
- // count the separators to determine which format to return
- int separatorCount = record.Count(f => f == ',');
- if (separatorCount == 3) // in Structure 1 there are three separators
- return typeof(Format1);
- else
- return typeof(Format2);
- }
- static void Main(string[] args)
- {
- MultiRecordEngine engine;
- engine = new MultiRecordEngine(typeof(Format1), typeof(Format2));
- engine.RecordSelector = new RecordTypeSelector(CustomSelector);
- /// MultiRecords.txt can contain records either of the form
- /// Field 1,Field 2,Field 3,Field 4
- /// or of the form
- /// Field 1,Field 4
- // read file - Normally you would use
- //object[] importedObjects = engine.ReadFile("MultiRecords.txt");
- // but for the sake of example we'll use engine.ReadString() instead
- object[] importedObjects = engine.ReadString(
- @"a,b,c,d" + Environment.NewLine +
- @"a,d");
- foreach (object importedObject in importedObjects)
- {
- if (importedObject is Format1)
- {
- Format1 format1 = (Format1)importedObject;
- // process it (for example, check the values)
- Assert.AreEqual("a", format1.Field1);
- Assert.AreEqual("b", format1.Field2);
- Assert.AreEqual("c", format1.Field3);
- Assert.AreEqual("d", format1.Field4);
- }
- else
- if (importedObject is Format2)
- {
- Format2 format2 = (Format2)importedObject;
- // process it (for example, check the values)
- Assert.AreEqual("a", format2.Field1);
- Assert.AreEqual("d", format2.Field4);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement