c# - Best practices for serializing objects to a custom string format for use in an output file -
About to apply override of toasting () on a particular business category, to create an Excel friendly format Was written in an output file, which will be picked up later and processed. What is the data given here:
5555555 "LASTN SR, FIRSTN" 5555555555 13956 STREET RD TOWNSVILLE MI 48890 25.88 01-003-06-0934
This is not just a big deal for me to create a format string and override When you need to CSV, tab-delimited or create, what do you do with any other non-XML straight from an object? Here is a normal fashion for creating CSV. A list of items, reflection use: There may be several variations, such as writing files directly to ToCsv (), or changing stringbilders with an IEnumerable and yield statement. ToString ()
, but it changes the behavior of ToString ()
for any object Now, I am studying, and to implement a class, it looks like a way tostring () > Good idea, but I am still I am a bit confused about whether it should be located where all logic and format class to be created.
public static string toCsv & l; T & gt; (String separator, INMEMERABLE & lt; T & gt; item list) {type T = typef (T); Fieldinfo [] field = t. Gatfield (); String Header = String .joint (Segmentation, Field. Select (F => f.Name) .Oir ()); Stringbilder csvdata = new stringbilder (); Csvdata.AppendLine (header); Foreach (var o in object) csvdata.AppendLine (ToCsvFields (Separator, Field, O)); Return csvdata.ToString (); } Public Fixed String ToCsvFields (string separator, FieldInfo [] field, object o) {StringBuilder linie = New StringBuilder (); Foreach (var f in fields) {if (linie.Length> gt; linie.Append (separator); Var x = f.GetValue (o); If (x! = Zero) linie.Append (x.ToString ()); } Return linie.ToString (); }
Comments
Post a Comment