KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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() ? Update: Using stream.Deserialize<List<MyClass.MyStruct>>() results in an error: 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() Update 2 (sample console app) - run once to create the file, again to read from it 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<T>(this T o) where T : new() { Stream stream = new MemoryStream(); BinaryFormatter bin = new BinaryFormatter();
Tags (comma-separated)
Save Edits
Cancel