title: Custom serialization of an object in .NET
body: <p>I have a requirement to serialize a list of objects to a flat file. The calls will be something like:</p>
  
  <pre><code>class MyObject
  {
      public int x;
      public int y;
      public string a;
      public string b;
  }
  </code></pre>
  
  <p>When I serialize this object, a record should be written in a ascii encoded flat file. Now, the length of field x should be 10 characters (right aligned), field y should be 20 characters (right aligned), fiels a should be 40 (left aligned) and field b should be 100 characters (left aligned).
  How can I achieve such a thing.</p>
  
  <p>A serialized object should look like :</p>
  
  <pre><code>        25                   8                                     akjsrj                                                                                          jug
  </code></pre>
  
  <p>I was thinking that may be I can apply custom attributes attributes to the fields and can decide at runtime how to serialize the field..</p>