Can I build a class as shown below dynamically using reflection? There are no methods, just public variables, some have custom attributes.
Is the .Emit method required (from what I've seen, "Emit" looks a little challenging).
I'm using software from www.FileHelpers.net, 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).
[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;
}
Update 1: Based on Vlad's answer below I needed to reference DLL, here's how I did it:
// need to reference the FileHelpers.dll from our own .exe directory
string diskFilenameFileHelpersDLL =
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location) +
@"\FileHelpers.dll";
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.
Assembly assembly = compiledResult.CompiledAssembly;
// Simple Data Test
lineContents = "John,Doe,[email protected]";
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 < 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);
}