c# - Best way to parse AlphaNumeric string, parts of which will be converted to Int, Bool, String -
i have schema according alphanumeric string of specific length interpreted. example, index 0-12 interpret string
, 13-20 interpreted int
, 21-22 bool
, on. far, have linked list formed @ runtime, each element(struct
) has 2 fields: length , type(enum
). iterating through list , checking type element belongs to, correct conversion performed.
seems method inefficient , error prone task. there better way ?
assuming stuck fixed-width data source (which should avoid if @ possible extremely non human-readable), use attributes directly decorate class data deserialized. like:
class somedto { [position(0,12)] public string somestring { get; set; } [position(13, 20)] public int someint { get; set; } [position(21-25)] public bool somebool { get; set; } }
and write deserializer picks type property directly , column position attribute. way, information deserializing kept in 1 place, reducing risks of typo errors.
Comments
Post a Comment