Advertisement
shamp00

Multiple CSV strutures with FileHelpers

Jan 12th, 2012
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FileHelpers;
  5. using NUnit.Framework;
  6.  
  7. namespace ConsoleApplication8
  8. {
  9.     class Program
  10.     {
  11.         [DelimitedRecord(",")]
  12.         public class Format1
  13.         {
  14.             public string Field1;          
  15.             public string Field2;            
  16.             public string Field3;            
  17.             public string Field4;
  18.         }
  19.  
  20.         [DelimitedRecord(",")]
  21.         public class Format2
  22.         {
  23.             public string Field1;
  24.             public string Field4;
  25.         }
  26.  
  27.         static Type CustomSelector(MultiRecordEngine engine, string record)
  28.         {
  29.             // count the separators to determine which format to return
  30.             int separatorCount = record.Count(f => f == ',');
  31.             if (separatorCount == 3) // in Structure 1 there are three separators
  32.                 return typeof(Format1);
  33.             else
  34.                 return typeof(Format2);
  35.         }
  36.  
  37.         static void Main(string[] args)
  38.         {
  39.             MultiRecordEngine engine;
  40.             engine = new MultiRecordEngine(typeof(Format1), typeof(Format2));
  41.             engine.RecordSelector = new RecordTypeSelector(CustomSelector);
  42.  
  43.             /// MultiRecords.txt can contain records either of the form
  44.             ///   Field 1,Field 2,Field 3,Field 4
  45.             /// or of the form
  46.             ///   Field 1,Field 4
  47.  
  48.             // read file - Normally you would use
  49.             //object[] importedObjects = engine.ReadFile("MultiRecords.txt");
  50.             // but for the sake of example we'll use engine.ReadString() instead
  51.  
  52.             object[] importedObjects = engine.ReadString(
  53.                 @"a,b,c,d" + Environment.NewLine +
  54.                 @"a,d");
  55.  
  56.             foreach (object importedObject in importedObjects)
  57.             {
  58.                 if (importedObject is Format1)
  59.                 {
  60.                     Format1 format1 = (Format1)importedObject;
  61.                     // process it (for example, check the values)
  62.                     Assert.AreEqual("a", format1.Field1);
  63.                     Assert.AreEqual("b", format1.Field2);
  64.                     Assert.AreEqual("c", format1.Field3);
  65.                     Assert.AreEqual("d", format1.Field4);
  66.  
  67.                 }
  68.                 else
  69.                 if (importedObject is Format2)
  70.                 {
  71.                     Format2 format2 = (Format2)importedObject;
  72.                     // process it (for example, check the values)
  73.                     Assert.AreEqual("a", format2.Field1);
  74.                     Assert.AreEqual("d", format2.Field4);
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement