title: Use Reflection to Build a Class (to build a dynamic FileHelper class)
body: <p>Can I build a class as shown below dynamically using reflection?  There are no methods, just public variables, some have custom attributes. </p>
  
  <p>Is the .Emit method required (from what I've seen, "Emit" looks a little challenging). </p>
  
  <p>I'm using software from <a href="http://www.filehelpers.net" rel="nofollow">www.FileHelpers.net</a>, and it requires a class.  All my file definitions are in a database table, and I'd like to make everything more dynamic (i.e. no code changes when a new column appears in the file).  </p>
  
  <pre><code>[FileHelpers.DelimitedRecord(",")]
  public class FileRow
  {
      [FileHelpers.FieldQuoted('"', QuoteMode.OptionalForBoth)] 
      public string Borrower_First_Name;
      [FileHelpers.FieldQuoted('"', QuoteMode.OptionalForBoth)] 
      public string Borrower_Last_Name;
      public string Borrower_Email;
  }
  </code></pre>
  
  <p>Update 1: Based on Vlad's answer below I needed to reference DLL, here's how I did it: </p>
  
  <pre><code>    // need to reference the FileHelpers.dll from our own .exe directory 
      string diskFilenameFileHelpersDLL = 
          System.IO.Path.GetDirectoryName(
             System.Reflection.Assembly.GetExecutingAssembly().Location) + 
             @"\FileHelpers.dll";
  </code></pre>
  
  <p>Update 2: Also, after doing what Vlad suggested, this is how I call FileHelper and loop through the results.  I'll probably transfer the data to a list. </p>
  
  <pre><code>    Assembly assembly = compiledResult.CompiledAssembly;
  
      // Simple Data Test 
      lineContents = "John,Doe,jd123456@yahoo.com";
      FileHelperEngine engine = new FileHelperEngine(assembly.GetType("FileRow"));
      // FileRow[] FileRowArray = (FileRow[])engine.ReadString(lineContents);
      Object[] FileRowArray = engine.ReadString(lineContents);
      Object myObject = FileRowArray[0];  // only 1 row of data in this example 
  
      // Get the type handle of a specified class.
      Type myType = assembly.GetType("FileRow");
      // Get the fields of the specified class.
      FieldInfo[] myField = myType.GetFields();
  
      Console.WriteLine("\nDisplaying fields values:\n");
      for (int i = 0; i &lt; myField.Length; i++)
      {
          Object objTest = myField.GetValue(i);
  
          string tempName = myField[i].Name;
          Object objTempValue = myField[i].GetValue(myObject);
          string tempValue = System.Convert.ToString(objTempValue);
  
          Console.WriteLine("The value of {0} is: {1}",
                              tempName, tempValue);
  
      }
  </code></pre>