Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. [DelimitedRecord(",")]
  2. public partial class Person : INotifyWrite<Person>
  3. {
  4. public string FirstName;
  5. public string LastName;
  6. [FieldOptional]
  7. public string Optional1;
  8. [FieldOptional]
  9. public string Optional2;
  10. [FieldOptional]
  11. public string Optional3;
  12.  
  13. public void BeforeWrite(BeforeWriteEventArgs<Person> e)
  14. {
  15. }
  16.  
  17. public void AfterWrite(AfterWriteEventArgs<Person> e)
  18. {
  19. // count the non-optional fields
  20. var numberOfNonOptionalFields = typeof(Person).GetFields()
  21. .Where(f => !f.GetCustomAttributes(false).Any(a => a is FieldOptionalAttribute))
  22. .Count();
  23.  
  24. // take only the first n tokens
  25. e.RecordLine = String.Join(",", e.RecordLine.Split(',').Take(numberOfNonOptionalFields));
  26. }
  27. }
  28.  
  29. class Program
  30. {
  31. private static void Main(string[] args)
  32. {
  33. var engine = new FileHelperEngine<Person>();
  34. var export = engine.WriteString(
  35. new Person[] {
  36. new Person() {
  37. FirstName = "Joe",
  38. LastName = "Bloggs",
  39. Optional1 = "Option 1",
  40. Optional2 = "Option 2",
  41. Optional3 = "Option 3"
  42. }
  43. });
  44. Assert.AreEqual("Joe,Bloggs" + Environment.NewLine, export);
  45. Console.WriteLine("Export was as expected");
  46. Console.ReadLine();
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement