Solution 1 - Super fast but unsafe:
- Create your class with
[StructLayout(LayoutKind.Sequential)] and all other unmanaged code markings for length. Your strings will be char array but can be exposed as string after loading.
- Read 180 bytes and create a byte array of the same size inside a
fixed block
- Change pointer to
IntPtr and use Marshal.PtrToStructure() to load an onject of your class
Solution 2 - Loading logic in the class:
- Create a constructor in your class that accepts
byte[] and inside the objects using Covenrt.Toxxx or Encoding.ASCII.ToString() assuming it is ASCII
- Read 180 bytes and create an object and pass it to .ctor
- If you have to serialise back to
byte[] then implement a ToByteArray() method and again use Covenrt.Toxxx or Encoding.ASCII.ToString() to write to byte.
Enhancement to solutions 2:
Create custom attributes and decorate your classes with those so that you can have a factory that reads metadata and inflates your objects using byte array for you. This is most useful if you have more than a couple of such classes.
Alternative to solutions 2:
You may pass stream instead of a byte array which is faster. Here you would use BinaryReader and BinaryWriter to read and write values. Strings however is a bit trick since it writes the length as well I think.