Click button to edit
Converters: Enum Converter
When you have a string field in your files that can be better handled if you map it to an enum.
Input.txt
ALFKI|Alfreds Futterkiste|Maria Anders|SalesRepresentative
ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|Owner
FRANR|France restauration|Carine Schmitt|MarketingManager
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner
CustomerTitle.cs
public enum CustomerTitle
{
Owner,
SalesRepresentative,
MarketingManager
}
Customers with Enum.cs
[DelimitedRecord("|")]
public class Customer
{
public string CustomerID;
public string CompanyName;
public string ContactName;
// Notice last feild is our enumerator
public CustomerTitle ContactTitle;
}
RunEngine.cs
public override void Run()
{
var engine = new DelimitedFileEngine<Customer>();
// Read input records, enumeration automatically converted
Customer[] customers = engine.ReadFile("Input.txt");
foreach (var cust in customers)
Console.WriteLine("Customer name {0} is a {1}", cust.ContactName, cust.ContactTitle);
}
Console
Customer name Maria Anders is a SalesRepresentative
Customer name Ana Trujillo is a Owner
Customer name Carine Schmitt is a MarketingManager
Customer name Antonio Moreno is a Owner