title: Write only stream - get written bytes count with DataContractSerializer
body: <p>Consider I have the following snippet:</p>
  
  <pre><code>public void Store(Stream s, object t)
  {
      var serializer = new DataContractSerializer(target.GetType(),
                                              new DataContractSerializerSettings
                                              {
                                                  PreserveObjectReferences = true
                                              });
  
      serializer.WriteObject(s, target);
  }
  </code></pre>
  
  <p>where <code>s</code> is <strong>write-only</strong> and <strong>doesn't support seeking</strong>.</p>
  
  <p>Is there any way to get bytes count that was written to stream by <code>WriteObject</code>? I know I can do it in the following way:</p>
  
  <pre><code>using (var memStream = new MemoryStream())
  {
      serializer.WriteObject(serializer, target);
      Debug.WriteLine(memStream.Length);
      memStream.CopyTo(s);
  }
  </code></pre>
  
  <p>but I wonder wheter it is possible to avoid <code>CopyTo</code> - object is quite huge.</p>
  
  <p><strong>EDIT:</strong>
  I've just came up with an idea: I can create a wrapper that counts bytes on write. It's the best solution so fat, but maybe there is another way.</p>
  
  <p><strong>DONE</strong></p>
  
  <p>I've implemented a wrapper: <a href="https://github.com/pwasiewicz/counted-stream" rel="nofollow">https://github.com/pwasiewicz/counted-stream</a> - maybe it will be useful for someone.</p>
  
  <p>Thanks!</p>