title: Deserialize Stream to List<T> or any other type
body: <p>Attempting to deserialize a stream to <code>List&lt;T&gt;</code> (or any other type) and am failing with the error:</p>
  
  <blockquote>
    <p>The type arguments for method <code>Foo.Deserialize&lt;T&gt;(System.IO.Stream)</code> cannot be inferred from the usage. Try specifying the type arguments explicitly.</p>
  </blockquote>
  
  <p>This fails:</p>
  
  <pre><code>public static T Deserialize&lt;T&gt;(this Stream stream)
  {
      BinaryFormatter bin = new BinaryFormatter();
      return (T)bin.Deserialize(stream);
  }
  </code></pre>
  
  <p>But this works:</p>
  
  <pre><code>public static List&lt;MyClass.MyStruct&gt; Deserialize(this Stream stream)
  {
      BinaryFormatter bin = new BinaryFormatter();
      return (List&lt;MyClass.MyStruct&gt;)bin.Deserialize(stream);
  }
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>public static object Deserialize(this Stream stream)
  {
      BinaryFormatter bin = new BinaryFormatter();
      return bin.Deserialize(stream);
  }
  </code></pre>
  
  <p>Is it possible to do this without casting, e.g. <code>(List&lt;MyStruct&gt;)stream.Deserialize()</code>?</p>
  
  <p><strong>Update:</strong><br>
  Using <code>stream.Deserialize&lt;List&lt;MyClass.MyStruct&gt;&gt;()</code> results in an error:</p>
  
  <blockquote>
  <pre><code>System.InvalidCastException: Unable to cast object of type 'System.RuntimeType'
  to type 'System.Collections.Generic.List`1[MyClass+MyStruct]'.
  at StreamExtensions.Deserialize[T](Stream stream)
  at MyClass.RunSnippet()
  </code></pre>
  </blockquote>
  
  <p><strong>Update 2 (sample console app)</strong> - run once to create the file, again to read from it  </p>
  
  <pre><code>using System;
  using System.IO;
  using System.Collections.Generic;
  using System.Runtime.Serialization;
  using System.Runtime.Serialization.Formatters;
  using System.Runtime.Serialization.Formatters.Binary;
  
  public static class StreamExtensions
  {
      public static Stream Serialize&lt;T&gt;(this T o) where T : new()
      {
          Stream stream = new MemoryStream();
          BinaryFormatter bin = new BinaryFormatter();
          bin.Serialize(stream, typeof(T));
          return stream;
      }
  
      public static T Deserialize&lt;T&gt;(this Stream stream) where T : new()
      {
          BinaryFormatter bin = new BinaryFormatter();
          return (T)bin.Deserialize(stream);
      }
  
      public static void WriteTo(this Stream source, Stream destination)
      {
          byte[] buffer = new byte[32768];
          source.Position = 0;
          if(source.Length &lt; buffer.Length) buffer = new byte[source.Length];
          int read = 0;
          while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
          {
              destination.Write(buffer, 0, read);
          }
      }
  }
  
  
  public class MyClass
  {
      public struct MyStruct
      {
          public string StringData;
          public MyStruct(string stringData)
          {
              this.StringData = stringData;
          }
      }
  
      public static void Main()
      {
          // binary serialization
          string filename_bin = "mydata.bin";
          List&lt;MyStruct&gt; l;
          if(!File.Exists(filename_bin))
          {
              Console.WriteLine("Serializing to disk");
              l = new List&lt;MyStruct&gt;();
              l.Add(new MyStruct("Hello"));
              l.Add(new MyStruct("Goodbye"));
              using (Stream stream = File.Open(filename_bin, FileMode.Create))
              {
                  Stream s = l.Serialize();
                  s.WriteTo(stream);
              }
          }
          else
          {
              Console.WriteLine("Deserializing from disk");
              try
              {
                  using (Stream stream = File.Open(filename_bin, FileMode.Open))
                  {
                      l = stream.Deserialize&lt;List&lt;MyStruct&gt;&gt;();
                  }
              }
              catch(Exception ex)
              {
                  l = new List&lt;MyStruct&gt;();
                  Console.WriteLine(ex.ToString());
              }
          }
  
          foreach(MyStruct s in l)
          {
              Console.WriteLine(
                  string.Format("StringData: {0}",
                      s.StringData
                  )
              );
          }
  
          Console.ReadLine();
      }
  }
  </code></pre>