将流反序列化为列表<t>或任何其他类型</t>

时间:2010-06-01 12:21:06

标签: c# generics extension-methods

尝试将流反序列化为List<T>(或任何其他类型)并且因错误而失败:

  

无法从用法中推断出方法Foo.Deserialize<T>(System.IO.Stream)的类型参数。尝试明确指定类型参数。

这失败了:

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();
    }
}

3 个答案:

答案 0 :(得分:8)

我假设你正在调用你的扩展方法:

List<MyStruct> result = mystream.Deserialize();    

在这种情况下,编译器无法确定T的{​​{1}}(它不会查看方法调用结果所分配给的变量)。

所以你需要明确指定type参数:

Deserialize

这有效:

List<MyStruct> result = mystream.Deserialize<List<MyStruct>>();

答案 1 :(得分:1)

您可以使用原始通用方法,只需明确指定泛型类型......

stream.Deserialize<List<MyClass.MyStruct>>();

答案 2 :(得分:1)

您正在序列化列表的类型而不是实际列表。它应该是:

bin.Serialize(stream, o)

此外,您必须将MyStruct标记为Serializable才能正确序列化。

相关问题