Deserialize Stream to List<T> or any other type
Posted
by Sam
on Stack Overflow
See other posts from Stack Overflow
or by Sam
Published on 2010-06-01T12:21:06Z
Indexed on
2010/06/01
12:23 UTC
Read the original article
Hit count: 282
Attempting to deserialize a stream to List<T>
(or any other type) and am failing with the error:
The type arguments for method 'Foo.Deserialize<T>(System.IO.Stream)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
This fails:
public static T Deserialize<T>(this Stream stream)
{
BinaryFormatter bin = new BinaryFormatter();
return (T)bin.Deserialize(stream);
}
But this works:
public static List<MyClass.MyStruct> Deserialize(this Stream stream)
{
BinaryFormatter bin = new BinaryFormatter();
return (List<MyClass.MyStruct>)bin.Deserialize(stream);
}
or:
public static object Deserialize(this Stream stream)
{
BinaryFormatter bin = new BinaryFormatter();
return bin.Deserialize(stream);
}
Is it possible to do this without casting, e.g. (List<MyStruct>)stream.Deserialize()
?
© Stack Overflow or respective owner