List<T>
को एक धारा (या किसी भी अन्य प्रकार) deserialize करने का प्रयास और त्रुटि के साथ विफल रहा:deserialize स्ट्रीम <T> या किसी भी अन्य प्रकार सूची में
The type arguments for method
Foo.Deserialize<T>(System.IO.Stream)
cannot be inferred from the usage. Try specifying the type arguments explicitly.
यह विफल रहता है:
public static T Deserialize<T>(this Stream stream)
{
BinaryFormatter bin = new BinaryFormatter();
return (T)bin.Deserialize(stream);
}
लेकिन इस काम करता है:
public static List<MyClass.MyStruct> Deserialize(this Stream stream)
{
BinaryFormatter bin = new BinaryFormatter();
return (List<MyClass.MyStruct>)bin.Deserialize(stream);
}
या:
public static object Deserialize(this Stream stream)
{
BinaryFormatter bin = new BinaryFormatter();
return bin.Deserialize(stream);
}
क्या यह बिना कास्टिंग किए ऐसा करना संभव है, उदा। (List<MyStruct>)stream.Deserialize()
?
अद्यतन:
का उपयोग stream.Deserialize<List<MyClass.MyStruct>>()
परिणाम एक त्रुटि में:
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()
अद्यतन 2 (नमूना सांत्वना एप्लिकेशन) - फ़ाइल बनाने के लिए एक बार फिर से चलाएं इसे से
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();
bin.Serialize(stream, typeof(T));
return stream;
}
public static T Deserialize<T>(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 < 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<MyStruct> l;
if(!File.Exists(filename_bin))
{
Console.WriteLine("Serializing to disk");
l = new List<MyStruct>();
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<List<MyStruct>>();
}
}
catch(Exception ex)
{
l = new List<MyStruct>();
Console.WriteLine(ex.ToString());
}
}
foreach(MyStruct s in l)
{
Console.WriteLine(
string.Format("StringData: {0}",
s.StringData
)
);
}
Console.ReadLine();
}
}
पढ़ने के लिए
@ सैम: आपके सीरियलize विधि में एक बग है। आप ऑब्जेक्ट 'ओ' के बजाय' टाइपोफ (टी) '(जो एक रनटाइम टाइप है) को क्रमबद्ध कर रहे हैं! – dtb
था [Serializable], बस इसे नमूना में रखना भूल गया ... बग deserialization के बजाय serialization था (इसके बारे में नहीं सोचा था क्योंकि कोई अपवाद फेंक दिया गया था) – SamWM
@ डीटीबी: मुझे एक ही समस्या थी मेरा बाइनरीफॉर्मेटर, इसे इंगित करने के लिए धन्यवाद। XmlSerializer कन्स्ट्रक्टर में टाइपऑफ() का उपयोग करता है क्योंकि यह अनदेखा करना आसान है। – angularsen