public static FileType Read(Reader reader) { FileType result = new FileType(); try { result.Type = reader.Read<FileKind>(); } catch (SystemException ex) { throw new FormatException("can't read 'Type' field", ex); } switch (result.Type) { case FileKind.Text: break; case FileKind.Data: try { result.Creator = reader.ReadVar<string>(MaxNameLen); } catch (SystemException ex) { throw new FormatException("can't read 'Creator' field", ex); } break; case FileKind.Exec: try { result.Interpretor = reader.ReadVar<string>(MaxNameLen); } catch (SystemException ex) { throw new FormatException("can't read 'Interpretor' field", ex); } break; default: throw new FormatException("unexpected value: " + result.Type.ToString()); } return result; }
public static void Write(Writer writer, FileType item) { switch (item.Type) { case FileKind.Text: writer.Write<FileKind>(FileKind.Text); return; case FileKind.Data: writer.Write<FileKind>(FileKind.Data); writer.WriteVar<string>(MaxNameLen, item.Creator); return; case FileKind.Exec: writer.Write<FileKind>(FileKind.Exec); writer.WriteVar<string>(MaxNameLen, item.Interpretor); return; default: throw new InvalidOperationException("unexpected value"); } }