views:

15

answers:

1

I need to read/write files, following a format provided by a third party specification. The specification itself is pretty simple: it says the position and the size of the data that will be saved in the file. For example:

Position        Size        Description
--------------------------------------------------
0001            10          Device serial number
0011            02          Hour
0013            02          Minute
0015            02          Second
0017            02          Day
0019            02          Month
0021            02          Year

The list is very long, it has about 400 elements. But lots of them can be combined. For example, hour, minute, second, day, month and year can be combined in a single DateTime object. I've splitted the elements into about 4 categories, and created separeted classes for holding the data. So, instead of a big structure representing the data, I have some smaller classes. I've also created different classes for reading and writing the data.

The problem is: how to map the positions in the file to the objects properties, so that I don't need to repeat the values in the reading/writing class? I could use some custom attributes and retrieve them via reflection. But since the code will be running on devices with small memory and processor, it would be nice to find another way. My current read code looks like this:

public void Read() {
    DataFile dataFile = new DataFile();
    // the arguments are: position, size
    dataFile.SerialNumber = ReadLong(1, 10);
    //...
}

Any ideas on this one? Thanks!

A: 

Custom attributes was going to be my suggestion, but I see you've already thought about that. Aside from that, my only other suggestion would be to store the mapping in, say, an XML file.

allonym