FileHelpers - Enum Converter

  • Contact / Feedback
  • Introduction
  • Download
  • Quick Start
  • Must Read
  • Examples
    • QuickStart
      • Read Delimited File
      • Write Delimited File
      • Read Fixed File
      • Write Fixed File
      • Read or Write Record by Record
      • Autoproperties
    • Missing Values
      • Handle Missing Values with Nullable
      • Handle Missing Values With FieldNullValue
    • Attributes
      • FieldTrim
      • FieldOrder
      • FixedLengthRecord FixedMode.AllowLessChars
    • Converters
      • Enum Converter
      • Custom Converter
    • Events And Notification
      • INotifyRead Interface
      • INotifyWrite Interface
      • Before/After Read Event Handling
      • Before/After Write Event Handling
    • ErrorHandling
      • ErrorMode.ThrowException
      • ErrorMode.IgnoreAndContinue
      • ErrorMode SaveAndContinue
    • Advanced
      • Dynamic Engine Options
      • Multiple Delimiters
      • Multi Record Processing
      • Smart Format Detector
    • Sorting
      • Sort Big File with Record Class
      • Sort Big File without Record Class 1
      • Sort Big File without Record Class 2
    • MasterDetail
      • Master Detail Custom Selector
      • Master Detail Common Selector
  • Credits
  • Invite us a beer
  • Contribute
  • Source Code
  • Api Docs (Sandcastle)
  1. Examples
  2. Converters
  3. Enum Converter
Previous: FixedLengthRecord FixedMode.AllowLessChars
Next: Custom Converter
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
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs