title: How can one to dynamically parse a CSV file using C# and the Smart Format Detector in FileHelpers 3.1?
body: <p>As per in this <a href="http://www.filehelpers.net/example/Advanced/SmartFormatDetector/" rel="nofollow">FileHelpers 3.1 example</a>, you can automatically detect a CSV file format using the FileHelpers.Detection.SmartFormatDetector class.</p>
  
  <p>But the example goes no further. How do you use this information to dynamically parse a CSV file? It must have something to do with the DelimitedFileEngine but I cannot see how.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I figured out a possible way but had to resort to using reflection (which does not feel right). Is there another/better way? Maybe using System.Dynamic? Anyway, here is the code I have so far, it ain't pretty but it works:</p>
  
  <pre><code>        // follows on from smart detector example
  
          FileHelpers.Detection.RecordFormatInfo lDetectedFormat = formats[0];
  
          Type lDetectedClass = lDetectedFormat.ClassBuilderAsDelimited.CreateRecordClass();
  
          List&lt;FieldInfo&gt; lFieldInfoList = new List&lt;FieldInfo&gt;(lDetectedFormat.ClassBuilderAsDelimited.FieldCount);
          foreach (FileHelpers.Dynamic.DelimitedFieldBuilder lField in lDetectedFormat.ClassBuilderAsDelimited.Fields)
              lFieldInfoList.Add(lDetectedClass.GetField(lField.FieldName));
  
          FileHelperAsyncEngine lFileEngine = new FileHelperAsyncEngine(lDetectedClass);
          int lRecNo = 0;
          lFileEngine.BeginReadFile(cReadingsFile);
          try
          {
              while (true)
              {
                  object lRec = lFileEngine.ReadNext();
                  if (lRec == null)
                      break;
  
                  Trace.WriteLine("Record " + lRecNo);
                  lFieldInfoList.ForEach(f =&gt; Trace.WriteLine("   " + f.Name + " = " + f.GetValue(lRec)));
  
                  lRecNo++;
              }
          }
          finally
          {
              lFileEngine.Close();
          }
  </code></pre>