2

I am parsing BgMax file (normal text file separated by rows each property has a specific length in row) with Filehelpers library and I have succesfully created objects and records with FixedLengthRecord. However I would like to store the whole line for each record as a string not only the properties as objects. Is there a neat way like adding a FieldHidden property or something to the Record class so that it saves the whole line there automatically? I also thought to save that line when I am done with creating the Record object but I think it is a bit complicated / not a good idea to do this as I think I need to access the property through reflection.

As an example here is one of the record classes that I use

[FixedLengthRecord(FixedMode.AllowMoreChars)]
   public class StartRecord
   {
      [FieldFixedLength(2)]
      public string PostType;

      [FieldFixedLength(20)]
      public string LayoutName;

      ...

      [FieldFixedLength(1)]
      public string TestMarker;
   }

On this StartRecord class I would like to have another string property named Source that will include the whole line before converted into object. The object is being populated from this code and it is pretty easy to get that line since it the record comes from a foreach loop of list of records.

switch (record.Substring(0, 2))
{
   case "01":
       return typeof (StartRecord);                
} 

So I could maybe populate the property here but I do not like it that much.

Agraell
  • 83
  • 11
  • If you need the lines and not the deserialized objects, why not just read the lines of the file instead of objects? You need to make a more specific question. We have no idea what you FileHelpers library code looks like. Maybe you should add some of your code and it will become clearer? – mortb Nov 24 '15 at 14:57
  • The Filehelpers library can be found here http://www.filehelpers.net/ and also in the description of the tag that I used. I included also some code but I think the implementation of doing this is pretty straightforward. – Agraell Nov 24 '15 at 15:13
  • The "easiest" soultion might be to write something like: `public Soutrce {get { return PostType + LayoutName + TestMarker}}` and you could of course do reflection. You could modify the code here: http://stackoverflow.com/questions/531384/how-to-loop-through-all-the-properties-of-a-class – mortb Nov 24 '15 at 15:20
  • I have seen that thread but I would consider that as a last resort method. I would like to see first if it is possible to do it from the class itself and use native FileHelpers decorations. – Agraell Nov 24 '15 at 15:30
  • Sorry that I have not worked with FileHelpers... I saw in this thread that filehelpers may parse values into an array http://stackoverflow.com/questions/2356419/filehelpers-and-csv-what-to-do-when-a-record-can-expand-unbounded-horizontally Why not parse the row into an array proprerty and then have the properties return the right index of the array: `public string PostType {get {return ArrayProperty[0]}}` and `public string Source {get {String.Join("", ArrayProperty)} }` That way you will have no extra storage needed. The String.Join is probably goo performancewise whithout keeping entire row – mortb Nov 24 '15 at 15:42
  • Thanks for your help but I think MarcosMeli answer is what I am looking for, even thought it is not working yet. – Agraell Nov 24 '15 at 21:42

1 Answers1

1

You can use the events interface:

[FixedLengthRecord(FixedMode.AllowMoreChars)]
public class StartRecord
    : INotifyRead
{
  [FieldFixedLength(2)]
  public string PostType;

  ...

  // Field hidden to the lib
  [FieldHidden]
  public string Source;


  public void BeforeRead(BeforeReadEventArgs e)
  {
      this.Source = e.RecordLine;
  }

  public void AfterRead(AfterReadEventArgs e)
  {   
  }

}
Marcos Meli
  • 3,468
  • 24
  • 29