arrays - Having trouble finding an easy way to parse out object properties in fixed width file in C# -
i'm having trouble finding nice way parse out object have in c# in fixed width format. object has various properties. found cleanest way parse out data use string formatting, need lengths of each field. have static class set this:
public static class lengths { public static int first_name = 15; public static int last_name = 25; public static int address1 = 25; etc... } the lengths placed array. [15, 25, 25]
then data fields placed array in same order:
string[] info = { obj.firstname, obj.lastname, obj.address1, etc...}; i should mention every time info string exact same, , not change overall unless change order, in case changed every time.
they passed parser made finds length of field, , how long should length array, , inserts blank spaces accordingly using string.format.
fixedwidthparser(info, lengths); the issue having maintain order both of these arrays. in event have change order, i'd have change order of both arrays.
am going wrong way? there better way this? if so, can point me in right direction?
one possible solution create custom attribute width , add on fields in obj type. somewhere store field names in array provide order serialization. can use reflection fields name, length attribute on field, , serialize [theorectically] same way you're doing now.
to field name:
type.getfield(string, bindingflags)
https://msdn.microsoft.com/en-us/library/4ek9c21e(v=vs.110).aspx
to attributes of field:
Comments
Post a Comment